microsoft / microsoft/TypeScript

import ConstJson from './config.json' as const;

Offen
#32,063 112 Kommentare 1829 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

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:

Search Terms

json const assertion import attributes with allowArbitraryExtensions

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit der Durchsicht der Issue-Diskussion, der Dokumentation zu Import-Attributen in TypeScript 5.3 und des bestehenden Ansatzes mit allowArbitraryExtensions. Erledigt ist die Aufgabe, wenn ein JSON-Import unter Verwendung des vorgeschlagenen const-Attributs Literalwerte wie "FR" | "BE" inferieren kann, ohne bestehende Imports oder das emittierte JavaScript zu ändern.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
javascript, typescript
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Ruhig
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.