microsoft / microsoft/TypeScript
Probabilistic Types
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
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.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit der Überprüfung von TypeScript's union-type checking und des für autocomplete und linting beschriebenen Tooling-Verhaltens. Ermittle, wie literal members als Hinweise verfügbar bleiben könnten, während der umschließende Typ sein aktuelles type-safety-Verhalten beibehält. Als abgeschlossen gilt die Arbeit, wenn der Vorschlag ein abgestimmtes Design enthält und den type checker, das IDE tooling sowie das optionale linter-Verhalten abdeckt.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers, developer-experience
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 25/100