Skip to content

Erroneous type narrowing in finally #18478

Closed
@Zbyl

Description

@Zbyl

TypeScript Version: Version 2.6.0-dev.20170914

Code

function test() {
    type YesNo = ('No' | 'Yes');
    let answer: YesNo = 'No'; // Bug disappears after adding 'as YesNo'.
    try {
        answer = 'Yes';
        return; // Bug disappears after removing the return.
    } finally {
        if (answer === 'Yes') { // Bug: Operator === cannot be applied to types 'No' and 'Yes'
        } else if (answer === 'No') {
        }
    }
}

Expected behavior:
No compilation errors.

Actual behavior:
Compilation error:
bug.ts(8,13): error TS2365: Operator '===' cannot be applied to types '"No"' and '"Yes"'.

Note:
This bug is similar to "Erroneous control flow type narrowing in loops": #15835

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions