-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Narrowing by checking for existence of key leads to never. #38608
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
It would be super cool if the type inside the |
Duplicate of #21732? Or related anyway |
#21732 would be very cool to have and resolving that may (as a side effect) resolve this (depending how it is implemented), but I don't think this is a duplicate of that or needs to block on that necessarily. |
This looks to be similar to #21517. There was some discussion here. It's not quite clear what the behaviour should be for the singleton case. For a union I think it is clear you want to narrow down to A proposed fix is to not apply |
@ahejlsberg thoughts on desired behavior? |
Further simplified repro case: const apple: {} = { a: 'hello' }
if ('a' in apple) {
apple // type: never
} else {
apple // type: {}
} The issue here is that just because |
@RyanCavanaugh I think our current behavior is quite odd. There's some good discussion in #21732, and I like @jcalz's suggestion of extending the current behavior to intersect with |
TypeScript Version: 3.9.2
Search Terms:
instanceof narrow derived
Code
Expected behavior:
error
inside theif
block is still of typeError
Actual behavior:
error
inside theif
block is of typenever
Playground Link: https://www.typescriptlang.org/play?#code/MYGwhgzhAEDCCuEAuB7AtgUQE5ZV6ApgB5IEB2AJjNrvgN6ElZgBc0yWAlmQObQC80AOQALAiBAoh0AL4AoYCjLJCOPGxp4B0MgQDucRKkxqsACgCUczgDNoZocSTNp3VbQvQ6c6L-daAegDoJABPAAcCNl0ANwIsORlCEAgCLx8-eNpoIJCIqOhNBJkgA
Related Issues:
None that seemed to match.
The text was updated successfully, but these errors were encountered: