microsoft / microsoft/TypeScript
This narrowing typeguard effect bleeds into subsequent statments on a type with bivariant type-parameter
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
TypeScript Version: 3.4.0-dev.201xxxxx (although 3.3 is also impacted)
Search Terms: this type guards
If we create a type-guard that narrows this on a type that has a type-parameter that is present only in a bivariant position, the effect of the type guard persists outside of the guarded block.
Code
type GetKnownKeys<G> = G extends GuardedMap<infer KnownKeys> ? KnownKeys: never;
interface GuardedMap<KnownKeys extends string> {
get(k: KnownKeys): number;
has<S extends string>(k: S): this is GuardedMap<S | GetKnownKeys<this>>;
}
declare let map: GuardedMap<never>;
map.get('bar') // err, as expected
if (map.has('foo')) {
map.get('foo').toExponential(); // ok as expected
if(map.has('bar'))
{
map.get('foo').toExponential(); // ok as expected
map.get('bar').toExponential(); // ok as expected
}
map.get('bar').toExponential(); /// OK!?!?! WHY ?!
}
map.get('bar') // OK ?!
Expected behavior:
Type guard only impacts the guarded block.
Actual behavior:
The effect of the type guard bleads into all subsequent statements. (marked with OK!?!?! and OK?!)
Note: With strictFunctionTypes on, declaring get as get: (k: KnownKeys) => number; makes the code work as expected.
Playground Link: link
Related Issues: Similar to #14817
Found this while playing with a solution for #9619
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
Riproduci la perdita di narrowing nel TypeScript Playground collegato usando l'esempio GuardedMap fornito. Traccia la gestione del control flow del compilatore per i type guard di this e i parametri di tipo bivarianti; il lavoro è completato quando il narrowing rimane confinato al blocco protetto e map.get('bar') dopo il blocco viene rifiutato, con copertura di regressione.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Specificata chiaramente
- Idoneità per principianti
- 38/100