microsoft / microsoft/TypeScript
Probabilistic Types
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
Search Terms
Probabilistic, union, supertypes
Suggestion
If a union type contains a member which is a supertype of one or more other members, the supertype subsumes those other members, but the information isn't lost. Tooling can then use that information as a hint about the expected distribution of possible values within that type.
type MetaSyntacticVar = 'foo' | 'bar' | 'baz' | string;
For the purposes of type safety, the above type is simply string. However, tools may consider 'foo' | 'bar' | 'baz' to be the most likely values for MetaSyntacticVar. An IDE might use them for autocomplete. A linter (i.e. not tsc) might flag other values as a warning. Etc.
Use Cases
A backend service offers a range of options, and I want to statically model them as a union type. Sometimes, the service adds a new option. I must either generalize the type to a string, do a type cast and tolerate some type unsafety, or release code updates in sync with all backend updates.
Examples
type MetaSyntacticVar = 'foo' | 'bar' | 'baz' | string;
const name: MetaSyntacticVar = 'qux';
================================^ IDE suggests foo, bar, or baz as I type this
================================^^^ eslint flags this as a warning (if I opt in)
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.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia esaminando il union-type checking di TypeScript e il comportamento degli strumenti descritto per autocomplete e linting. Determina come i literal members potrebbero rimanere disponibili come suggerimenti, mantenendo al contempo l'attuale comportamento di type-safety del tipo contenitore. Il lavoro è completo quando la proposta presenta un design concordato e copre il type checker, l'IDE tooling e il comportamento opzionale del linter.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers, developer-experience
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 25/100