Skip to content

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

Closed
@sweko

Description

@sweko

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'

Activity

mhegazy

mhegazy commented on Mar 1, 2017

@mhegazy
Contributor

did you mean 3 || null ?

sweko

sweko commented on Mar 2, 2017

@sweko
Author

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

gcnew commented on Mar 2, 2017

@gcnew
Contributor

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.

locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @sweko@gcnew@mhegazy

        Issue actions

          error TS2531: Object is possibly 'null' when using | operator (strictNullChecks are off) · Issue #14380 · microsoft/TypeScript