Skip to content

T | U var with initial type T, set to a U inside lambda always deduced as T #10613

Closed
@zenmumbler

Description

@zenmumbler

TypeScript Version: 2.0.2 (RC)

Code

function test(lines: string[]) {
    var curThing: { x: number } | null = null;

    lines.forEach(line => {
        curThing = { x: 1 };
    });

    if (curThing) {
        console.info(curThing.x);
    }
}

Expected behavior:

No errors.

Actual behavior:

When strict null checks are on, TS2 RC deduces that curThing in the if at the end is of type null and inside the block is of type never. The error I get then is that never has no member named x. Fair enough.

In this case I can sidestep the issue by using for (var line of lines) { … } instead of the forEach method, but that may not always be the case. BTW, this is a reduced case from some parser code.

This case also deals with T | null but this applies to any T | U type value. I tested this separately with a string | number initialised to a number and then set to a string, etc.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDesign LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions