Skip to content
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

"Object is possibly null" After Non-Null Assertion #37798

Closed
ChayimFriedman2 opened this issue Apr 5, 2020 · 5 comments
Closed

"Object is possibly null" After Non-Null Assertion #37798

ChayimFriedman2 opened this issue Apr 5, 2020 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@ChayimFriedman2
Copy link

TypeScript Version: 3.7.x-dev.201xxxxx

Search Terms:

typescript object is possibly null after non null assertion
typescript object is possibly null after exclamation mark check

Code

const obj: { a: number } | null = null;

if (obj!.a) {
    console.log(obj.a);
}

Expected behavior:

Successful compilation, since we notify the compiler with non-null assertion at line 3 that obj is not null, then it's not null at line 4 too.

Actual behavior:

Error "Object is possibly null" at line 4 (console.log(obj.a)).

Playground Link: https://www.typescriptlang.org/play?ts=3.9.0-dev.20200328#code/MYewdgzgLgBCBGArAXDA3jAhqsBXAtvAKYBOMAvjAD4x4A2dMAvLbgwNwBQnAlgGYwAFAkQBCAHSYAlOk4x5MUJBB0i4uiADmwpJKldycmEA

Related Issues:

@nicojs
Copy link

nicojs commented Apr 6, 2020

@ChayimFriedman2
Copy link
Author

@nicojs No. I mean non-null assertion.

I know that works with optional chaining operator, but that's not the case. It should produce no errors even with non-null assertion, since we let the compiler know (at line 3) that obj, not matter what you (= the compiler) knows about it, is NOT null. And since we don't assign null to obj between line 3 and 4, if in line 3 it can't be null - it can't be at line 4 too.

@nicojs
Copy link

nicojs commented Apr 6, 2020

I'm pretty sure the non-null assertion operator works as intended here. With non-null assertion you're effectively saying to TypeScript "I know it might be nullish, but I know better, please let me use it this one time". It doesn't interfere with control flow analysis.

Although this is not explicitly found in the handbook: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html#non-null-assertion-operator. Maybe adding this small example could help with that?

@ChayimFriedman2
Copy link
Author

ChayimFriedman2 commented Apr 6, 2020

@nicojs What's the point? If obj is null, it'll crash. If not, why do we emit an error in the next statement?

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Apr 13, 2020
@RyanCavanaugh
Copy link
Member

See #9640

In general we don't do "if it didn't crash" reasoning since it's very difficult to fit into an efficient control flow analysis, which is really what you're asking for since the same reasoning would apply to this example:

const obj: { a: number } | null = null;

if (obj!.a) {
}
console.log(obj.a);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants