microsoft / microsoft/TypeScript
import ConstJson from './config.json' as const;
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.3k
- Merge medio
- 2 d 4 h
- PR fusionados (30 d)
- 132
Descripción
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
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza revisando la discusión del issue, la documentación de import-attributes de TypeScript 5.3 y el enfoque existente de allowArbitraryExtensions. Se considera terminado cuando una importación JSON usando el atributo const propuesto puede inferir valores literales como "FR" | "BE" sin cambiar las importaciones existentes ni el JavaScript emitido.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- javascript, typescript
- Área
- compilers
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Tranquilo
- Claridad
- Bastante claro
- Aptitud para principiantes
- 35/100