microsoft / microsoft/TypeScript
Consider property access a form of type guards
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
This is a proposal for using property access as another form of type guards (see #900) to narrow union types. While we're investigating expanding the power of type guards (#1007) this feature would support the natural style that JavaScript programmers have in their code today.
Using property access to narrow union types
var x: string|number;
if (x.length) { // no error even though number lacks a length property
var r = x.charAt(3); // x is of type string inside this block
}
var r2 = x.length || x * x; // no error, x narrowed to number on right hand side, r3 is number
var y: string[]|number[]|string;
if (y.length && y.push) {
// y is string[]|number[] now
var first = y[0]; // first is string|number
if (first.length) {
// first is string in here
} else {
// first is number in here
}
}
We do not expand the situations in which types are narrowed, but we do expand the known type guard patterns to include basic property access. In these narrowing contexts it would be an error to access a property that does not exist in at least one of the constituent types of a union type (as it is today). However, it would now be valid to access a property that exists in at least one, but not all, constituent types. Any such property access will then narrow the type of the operand to only those constituent types in the union which do contain the accessed property. In any other context property access is unchanged.
Invalid property access
var x: string|number;
var r = x.length; // error, not a type guard, normal property access rules apply
if (x.len) { } // error, len does not exist on type string|number
var r3 = !x.len && x.length; // error, len does not exist on type string|number
var r4 = x.length && x.len; // error, len does not exist on type string
Issues/Questions
- Should the language service behave differently in these type guard contexts? Should dotting off a union type in a type guard list all members on all types rather than only those that exist on all of them?
- Need to understand performance implications
I have a sample implementation and tests in a branch here: https://github.com/Microsoft/TypeScript/tree/typeGuardsViaPropertyAccess. There're a couple bugs remaining but examples like the above all work and no existing behavior was changed.
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 dal branch typeGuardsViaPropertyAccess collegato nell’issue e verifica la sua implementazione di esempio e i test. Confronta gli esempi e i casi di accesso non valido in questa issue con il comportamento esistente dei type guard discusso in #900 e #1007. Il lavoro sarà completato quando saranno risolti i bug rimanenti, saranno affrontate le questioni relative al language service e alle prestazioni e saranno aggiunti o aggiornati i test per le regole di narrowing proposte.
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
- 20/100