Suggestion
The ability to get const types from a JSON configuration file.
If the json is:
{
"appLocales": ["FR","BE"]
}
I want to import the JSON and get a {appLocales: "FR" | "BE"} type instead of string
As suggested here, this could rely on ES import attributes (stage 4, ES2025, TypeScript 5.3):
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 explicitly using allowArbitraryExtensions: true 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:
Links:
This feature has been mentioned:
Search Terms
json const assertion import attributes with allowArbitraryExtensions
Suggestion
The ability to get const types from a JSON configuration file.
If the json is:
{ "appLocales": ["FR","BE"] }I want to import the JSON and get a
{appLocales: "FR" | "BE"}type instead ofstringAs suggested here, this could rely on ES import attributes (stage 4, ES2025, TypeScript 5.3):
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 explicitly using
allowArbitraryExtensions: trueand a.d.json.tsfile, but this doesn't permit to infer the type from the JSON directly, requires explicit typing and maintenance.Checklist
My suggestion meets these guidelines:
Links:
This feature has been mentioned:
Search Terms
json const assertion import attributes with allowArbitraryExtensions