microsoft / microsoft/TypeScript
Simpler Expression of Complex Type Conditions
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
Multiple, type, condition, expression, no, extra, case, and, or
Suggestion
I often find myself repeating sub-branches of conditional types, which makes reasoning about control flow more difficult. I'd recommend the same logical operators to those of JS land, at the type-level.
Example
Let's say we have three boolean types––ConditionA, ConditionB and ConditionC––and we want to gather the number of true conditions in a new type Result, which will evaluate to 0 | 1 | 2 | 3 depending on the aforementioned conditions. The runtime equivalent can be expressed quite legibly:
const Result =
(conditionA && conditionB && conditionC)
? 3
: (
(conditionA && conditionB) ||
(conditionB && conditionC) ||
(conditionA && conditionC)
)
? 2
: (conditionA || conditionB || conditionC)
? 1
: 0;
The type-level equivalent is not as obvious in its meaning, and we end up repeating the potential results.
type Result =
ConditionA extends true
? ConditionB extends true
? ConditionC extends true
? 3
: 2
: ConditionC extends true
? 2
: 1
: ConditionB extends true
? ConditionC extends true
? 2
: 1
: ConditionC extends true
? 1
: 0;
While it nets more code, the following is––imo––easier to understand.
type Result =
(ConditionA extends true && ConditionB extends true && ConditionC extends true)
? 3
: (
(ConditionA extends true && ConditionB extends true) ||
(ConditionB extends true && ConditionC extends true) ||
(ConditionA extends true && ConditionC extends true)
)
? 2
: (ConditionA extends true || ConditionB extends true || ConditionC extends true)
? 1
: 0;
Ideally, the same rules from JS-land could apply to judging whether a type is a subtype of truthy, so we could do this:
type Result =
(ConditionA && ConditionB && ConditionC)
? 3
: (
(ConditionA && ConditionB) ||
(ConditionB && ConditionC) ||
(ConditionA && ConditionC)
)
? 2
: (ConditionA || ConditionB || ConditionC)
? 1
: 0;
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 gli esempi di tipi condizionali e la sintassi proposta �26�26 e || nell’issue. Non sono indicati file sorgente, test o punti di ingresso del compilatore; per considerare il lavoro completato sarebbe necessario risolvere la questione di progettazione del linguaggio e validare la funzionalità nel compilatore TypeScript e nei relativi test.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- javascript, 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
- 25/100