microsoft / microsoft/TypeScript
Reflect.has fails to act as type guard (should act same as "in" operator)
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.20190330
Search Terms:
Reflect.has
in operator
Code
const test1 = (a: { field: number } | {}) => (("field" in a) ? a.field : 0);
const test2 = (a: { field: number } | {}) => Reflect.has(a, "field") ? a.field : 0;
Expected behavior:
Both compile successfully. "in" operator acts as a type guard per https://github.com/Microsoft/TypeScript/issues/10485. Reflect.has should act the same as "in" operator here.
We'd like to use Reflect.has a lot more and this case holds us back. Thank you!
Actual behavior:
"in" operator line compiles; Reflect.has line does not compile. Error:
allPasos.ts:445:31 - error TS2339: Property 'field' does not exist on type '{} | { field: number; }'.
Property 'field' does not exist on type '{}'.
445 Reflect.has(a, "field") ? a.field : 0;
Playground Link:
Related Issues:
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 dalla riproduzione collegata in Playground e confronta il comportamento esistente di narrowing dell’operatore in con Reflect.has; leggi la issue correlata #10485 per il contesto. Il lavoro è completato quando l’espressione Reflect.has mostrata compila e si comporta come previsto dalla issue, con una copertura per la 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
- Abbastanza chiara
- Idoneità per principianti
- 38/100