-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Non-null assertion operator confuses flow analysis with type guards #25802
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
Duplicate of #15655 |
I actually think there is a deeper issue here, not just related to the assertion operator. I get the same odd behavior when using function testFails(t?: Test) {
if (isA(t && t.c)) {
console.log(t && t.c.data);
}
} Playground: here Which probably means that the null propagation operator in #16 will have the same problem when it lands, since it's just syntactic sugar around this latest example. |
|
Thanks @Andy-MS, I understand what the non-null assertion operator does. This bug is not a huge deal for manually-written code, but it's a pain for code generators, because generating these variable extractions adds a lot of complexity to the generator. My side concern is that the upcoming Should I file a separate, more generic issue that doesn't focus on any specific operator, and just on the fact that some expressions get their type narrowed by type guards (e.g. |
We should probably support |
TypeScript Version: 3.1.0-dev.20180717
Search Terms: null assert
Code
Expected behavior: Both functions compile correctly.
Actual behavior: The second function does not compile, because after checking that
t!.c
is of typeCaseA
, the compiler forgets.Note that
t
is not nullable, but that doesn't matter. This also fails with nested optional properties, or even if you make the function parameter optional.A workaround is to pull
t!
to a variable, and then run the rest of the function on that variable.Playground Link: Here
Related Issues:
The text was updated successfully, but these errors were encountered: