import ConstJson from './config.json' as const;
See original GitHub issueSearch 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:
- Created 4 years ago
- Reactions:655
- Comments:43 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I’m for option #3. This one looks similar to the current “as const” syntax:
It makes it more intuitive. Definitely a killer feature for config-based code if Typescript adopts it.
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 astring
instead of'boolean' | 'string' | 'number' | ...
.