Skip to content

strictNullChecks thinks a value may be null if null is assigned to it in its non-null branch #7630

Description

@Arnavion

TypeScript Version:

3853bb8 (master after merging #7140 )

Code

class Prop {
    value: any;
}

var x: Prop | null = null;
if (x === null) {
    ;
}
else if (x.value === null) { // Line 9
    x = null;
}

tsc -t es5 --strictNullChecks ./foo.ts

Expected behavior:

Compiles.

Actual behavior:

foo.ts(9,10): error TS2531: Object is possibly 'null' or 'undefined'.


If the x = null; assignment (line 10) is removed, it does compile, and x is correctly inferred to be non-null in the condition and body of the else-if block. But this assignment should not have any effect on the nullability of x in the else-if condition.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions