Skip to content

error TS2531: Object is possibly 'null' when using | operator (strictNullChecks are off) #14380

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

Closed
sweko opened this issue Mar 1, 2017 · 3 comments

Comments

@sweko
Copy link

sweko commented Mar 1, 2017

I'm getting errors compiling after an update to 2.2.1.
The tsconfig.json file does not specify strictNullChecks, and the problem persists even when strictNullChecks are explicitly set to false.

TypeScript Version: 2.2.1

Code

console.log( 3 | null);

Expected behavior:
Successful compilation
Actual behavior:
error TS2531: Object is possibly 'null'

@mhegazy
Copy link
Contributor

mhegazy commented Mar 1, 2017

did you mean 3 || null ?

@sweko
Copy link
Author

sweko commented Mar 2, 2017

3 || null works as expected, but 3 | null does not.

In the specific case where I've encountered the issue, it was indeed an error where I've meant to do || but did | instead.
Nonetheless, I've gotten a build error on upgrading typescript, and this is not mentioned anywhere as a breaking change.

@gcnew
Copy link
Contributor

gcnew commented Mar 2, 2017

It's caused by #13483 and is mentioned in What's new in TypeScript - Better checking for null/undefined in operands of expressions. Sure, it can be seen as a breaking change although the better interpretation would be an error that hadn't been previously caught.

@sweko sweko closed this as completed Mar 2, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants