question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

import ConstJson from './config.json' as const;

See original GitHub issue

Search Terms

json const assertion import

Suggestion

The ability to get const types from a json configuration file.

IE if the json is:

{
  appLocales: ["FR","BE"]
}

I want to import the json and get the type {appLocales: "FR" | "BE"} instead of string

Use Cases

Current approach gives a too broad type string. I understand it may make sense as a default, but having the possibility to import a narrower type would be helpful: it would permit me to avoid maintaining both a runtime locale list + a union type that contains the values that are already in the list, ensuring my type and my runtime values are in sync.

Checklist

My suggestion meets these guidelines:

  • This wouldn’t be a breaking change in existing TypeScript/JavaScript code
  • This wouldn’t change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn’t a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript’s Design Goals.

Links:

This feature has been mentionned:

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:655
  • Comments:43 (2 by maintainers)

github_iconTop GitHub Comments

42reactions
TheMrZZcommented, May 17, 2020

I’m for option #3. This one looks similar to the current “as const” syntax:

const x = {...} as const

It makes it more intuitive. Definitely a killer feature for config-based code if Typescript adopts it.

35reactions
Hawkbatcommented, Jul 3, 2019

This would be extremely useful for adding direct support for JSON schemas to TypeScript. This can technically be accomplished with generators right now, but it would be so much more elegant to be able to use mapped types (for example, https://github.com/wix-incubator/as-typed) to map an imported JSON schema to its corresponding TypeScript type. It isn’t currently possible to use this approach with a JSON import since the type property of each schema object will be a string instead of 'boolean' | 'string' | 'number' | ....

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to import JSON files in ES modules (Node.js) - Stefan Judis
Learn about the ways to read and import JSON files in Node.js ... import { readFile } from 'fs/promises'; const json = JSON.parse(...
Read more >
How can I add data from a JSON to a constant variable in ...
Use JSON.parse() to accomplish that goal. const json = fs.readFileSync("./json/data.json", "utf8"); const data = JSON.parse(json);.
Read more >
How to Set Up Environment Variables using JSON files with ...
import { EnvConfig } from './environment-model'; import envConfig from '../../env-config.json'; export const env = envConfig as EnvConfig;.
Read more >
ECMAScript proposal: JSON modules - 2ality
It lets us import JSON data as if it were an ECMAScript module. ... (B) const json = await response.json(); // (C) return...
Read more >
Reading and Writing Files With TypeScript - Upmostly
import * as fsPromise from 'fs/promises'; const file = await ... config.json', 'utf-8'); const config = JSON.parse(loadedConfig); ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found