microsoft / microsoft/TypeScript
import ConstJson from './config.json' as const;
Personne n'a encore pris cette issue.
- Langage dominant
- Go
- Étoiles
- 111k
- Forks
- 14.4k
- Merge moyen
- 1 j 19 h
- PR mergées (30 j)
- 117
Description
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:
- 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 mentioned:
- by @qm3ster and @maartenth https://github.com/microsoft/TypeScript/pull/29510#issuecomment-456833107
- by @m-b-davis https://github.com/Microsoft/TypeScript/issues/26552#issuecomment-496925984
Search Terms
json const assertion import attributes with allowArbitraryExtensions
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez par examiner la discussion de l’issue, la documentation de TypeScript 5.3 sur les import-attributes et l’approche existante avec allowArbitraryExtensions. Le travail est terminé lorsqu’un import JSON utilisant l’attribut const proposé peut inférer des valeurs littérales telles que "FR" | "BE" sans modifier les imports existants ni le JavaScript généré.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- javascript, typescript
- Domaine
- compilers
- Type d'issue
- Fonctionnalité
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Activité
- Calme
- Clarté
- Plutôt claire
- Accessibilité débutants
- 35/100