Skip to content

TS don't consider multi variable control flow analysis #23301

@tinganho

Description

@tinganho

TypeScript Version: 2.7.0-dev.201xxxxx

Search Terms:
multi variable control flow analysis typescript

Code

declare const cleft: number | undefined;
declare const cright: number | undefined;

if (typeof cleft === 'number') {
    cleft.toExponential();
}
else {
    cleft.toExpontential(); // Error
}

if (typeof cleft === 'undefined' &&
    typeof cright === 'undefined')
{
    cleft.toExponential(); // Error
}
else if (typeof cleft === 'undefined' &&
    typeof cright !== 'undefined')
{
    cleft.toExponential(); // Error
}
else if (typeof cleft !== 'undefined' &&
    typeof cright === 'undefined')
{
    cleft.toExponential();
}
else {
    cleft.toExponential(); // Error But shouldn't
}

Expected behavior:
No error on the last else body.

Actual behavior:
Error on the last else body.

Playground Link:
http://www.typescriptlang.org/play/#src=%0Adeclare%20const%20cleft%3A%20number%20%7C%20undefined%3B%0Adeclare%20const%20cright%3A%20number%20%7C%20undefined%3B%0A%0Aif%20(typeof%20cleft%20%3D%3D%3D%20'number')%20%7B%0A%20%20%20%20cleft.toExponential()%3B%0A%7D%0Aelse%20%7B%0A%20%20%20%20cleft.toExpontential()%3B%20%2F%2F%20Error%0A%7D%0A%0Aif%20(typeof%20cleft%20%3D%3D%3D%20'undefined'%20%26%26%0A%20%20%20%20typeof%20cright%20%3D%3D%3D%20'undefined')%0A%7B%0A%20%20%20%20cleft.toExponential()%3B%20%2F%2F%20Error%0A%20%20%20%20%0A%7D%0Aelse%20if%20(typeof%20cleft%20%3D%3D%3D%20'undefined'%20%26%26%0A%20%20%20%20typeof%20cright%20!%3D%3D%20'undefined')%0A%7B%0A%20%20%20%20cleft.toExponential()%3B%20%2F%2F%20Error%0A%7D%0Aelse%20if%20(typeof%20cleft%20!%3D%3D%20'undefined'%20%26%26%0A%20%20%20%20typeof%20cright%20%3D%3D%3D%20'undefined')%0A%7B%0A%20%20%20%20cleft.toExponential()%3B%0A%7D%0Aelse%20%7B%0A%20%20%20%20cleft.toExponential()%3B%20%2F%2F%20Error%0A%7D%0A

Related Issues:

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