microsoft / microsoft/TypeScript
Reflect.has fails to act as type guard (should act same as "in" operator)
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
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:
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked Playground reproduction and compare the existing in-operator narrowing behavior with Reflect.has; read the related issue #10485 for context. Done means the shown Reflect.has expression compiles and behaves as the issue expects, with coverage for the regression.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100