microsoft / microsoft/TypeScript

Allow checking json in conjunction with .d.json.ts files

Offen
#52,994 2 Kommentare 8 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

Suggestion

🔍 Search Terms

checkJson, check json, .d.json.ts

✅ Viability 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, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

So in TS 5.0 we can add .d.json.ts files to type JSON files, i.e.:

// config.d.json.ts

declare const config: {
    option?: string,
    option2?: number,
};

export default config;
// config.json
{
    "option": "foo",
    "option2": 12
}
// main.ts
import config from "./config.json" assert { type: "json" };

// config has type { option?: string, option2?: number }

This is nice, but it would be better if we could validate that the corresponding .json files do in fact actually match the type. I'd like to propose a new option, checkJson that when set validates types errors within JSON files.

For example suppose we have:

// config.d.json.ts
declare const config: {
    value: string,
};

export default config;
// config.json
{
    "value": 3
}

Then TypeScript would report a type error when running tsc on the project (or in the editor).

📃 Motivating Example

Pretty much anywhere manual JSON files are used in conjunction with .d.json.ts files would be useful. The point of TS is to help detect errors, extending this to JSON is a natural extension.

In particular this would make a particularly good alternative to JSON schema support as I proposed in another issue.

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 damit nachzuverfolgen, wie TypeScript Deklarationen von .d.json.ts und Importe von config.json behandelt, wenn tsc ausgeführt wird oder im Editor. Definiere das Verhalten der vorgeschlagenen Option checkJson anhand der Beispiele im Issue, einschließlich der Meldung eines Typfehlers, wenn JSON-Werte nicht mit der Deklaration übereinstimmen. Als erledigt gilt die Aufgabe, wenn entsprechende JSON-Dateien validiert werden, ohne das ausgegebene JavaScript zu ändern.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

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

Neue Issues direkt in Ihr Postfach

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