microsoft / microsoft/TypeScript

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

Aperta
#52,994 2 commenti 8 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia tracciando come TypeScript gestisce le dichiarazioni .d.json.ts e gli import di config.json quando si esegue tsc o nell'editor. Definisci il comportamento dell'opzione checkJson proposta usando gli esempi nell'issue, incluso il riportare un errore di tipo quando i valori JSON non corrispondono alla dichiarazione. Il lavoro è completato quando i file JSON corrispondenti vengono convalidati senza modificare il JavaScript emesso.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.