import ConstJson from './config.json' as const;
## Suggestion The ability to get const types from a JSON configuration file. If the json is: ```json { "appLocales": ["FR","BE"] } ``` I want to import the JSON and get a `{appLocales: "FR" | "BE"}` type instead of `string` As [suggested here](https://github.com/microsoft/TypeScript/issues/32063#issuecomment-1708847986), this could rely on ES import attributes (stage 4, ES2025, [TypeScript 5.3](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-3.html)): ```ts import data from "./data.json" with { type: "json", const: true }; ``` ## Use Cases The current approach gives a too broad type `string`. I understand it makes by 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. It is possible to [type a JSON file](https://www.totaltypescript.com/override-the-type-of-a-json-file) explicitly using [`allowArbitraryExtensions: true`](https://www.typescriptlang.org/tsconfig/#allowArbitraryExtensions) and a `.d.json.ts` file, but this doesn't permit to infer the type from the JSON directly, requires explicit typing and maintenance. ## Checklist My suggestion meets these guidelines: * [X] This wouldn't be a breaking change in existing TypeScript/JavaScript code * [X] This wouldn't change the runtime behavior of existing JavaScript code * [X
Posted by slorber on Jun 24, 2019, 3:53 PM
Tags
Related issues
- [Meta] Feedback on granular control over Copilot changes
- unexpected status 401 Unauthorized issue
- [MODEL] Measurable quality regression in Claude generation 5 (Fable 5 / Opus 5 / Sonnet 5): worse nonsense detection, ~2x verbosity, under-disclosed model fallb
- [CRITICAL] Subagent spawning and subagent pattern bugs trigger infinite recursion, infinite token usage, grossly inefficient token usage, and lost accumulated s
- Video stops playing after some minutes
- [MODEL] Claude verbose code comments by default — ignores instructions to stop.
Raise real-world problems, find people facing the same issue, and discuss solutions.3,500 issues0 comments
github (Software)
[Meta] Feedback on granular control over Copilot changes
unexpected status 401 Unauthorized issue
[MODEL] Measurable quality regression in Claude generation 5 (Fable 5 / Opus 5 / Sonnet 5): worse nonsense detection, ~2x verbosity, under-disclosed model fallb
[CRITICAL] Subagent spawning and subagent pattern bugs trigger infinite recursion, infinite token usage, grossly inefficient token usage, and lost accumulated s
Video stops playing after some minutes
[MODEL] Claude verbose code comments by default — ignores instructions to stop.
Scope Codex VS Code chats to the current workspace/project
Update on "Co-authored-by: Copilot" in commit messages
import ConstJson from './config.json' as const;
## Suggestion The ability to get const types from a JSON configuration file. If the json is: ```json { "appLocales": ["FR","BE"] } ``` I want to import the JSON and get a `{appLocales: "FR" | "BE"}` type instead of `string` As [suggested here](https://github.com/microsoft/TypeScript/issues/32063#issuecomment-1708847986), this could rely on ES import attributes (stage 4, ES2025, [TypeScript 5.3](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-3.html)): ```ts import data from "./data.json" with { type: "json", const: true }; ``` ## Use Cases The current approach gives a too broad type `string`. I understand it makes by 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. It is possible to [type a JSON file](https://www.totaltypescript.com/override-the-type-of-a-json-file) explicitly using [`allowArbitraryExtensions: true`](https://www.typescriptlang.org/tsconfig/#allowArbitraryExtensions) and a `.d.json.ts` file, but this doesn't permit to infer the type from the JSON directly, requires explicit typing and maintenance. ## Checklist My suggestion meets these guidelines: * [X] This wouldn't be a breaking change in existing TypeScript/JavaScript code * [X] This wouldn't change the runtime behavior of existing JavaScript code * [X
Comments (0)
No comments yet. Start the discussion.