Skip to content

TypeScript does not expect variable change in forEach #16928

Closed
@pallada-92

Description

@pallada-92

TypeScript Version: 2.4.1

Code

// strictNullChecks should be on
let a: number | null = null;
[1].forEach(() => { a = 1; });
if (a !== null) {
    console.log(a + 1);
}

Expected behavior:
Output '2'.

Actual behavior:
error TS2365: Operator '+' cannot be applied to types 'never' and '1'.

Actually, after the forEach loop the inferred type of 'a' remains to be 'null', although it may change during the loop.

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