Skip to content

Unexpected type narrowing in finally block with return in both try and catch block #35644

Closed
@s2terminal

Description

@s2terminal

TypeScript Version: 3.7.2

Search Terms: try catch finally return

Code

const main = () => {
    let hoge: string | undefined = undefined;
    try {
        hoge = 'hoge!';
        return; // Bug disappears after removing this return.
    } catch {
        return; // Bug disappears after removing this return.
    } finally {
        if (hoge) {
            hoge.length;
        }
        return;
    }
}

main();

Expected behavior:
In finally block, hoge's type is string | undefined

Actual behavior:
In finally block, hoge's type is only undefined, and Property 'length' does not exist on type 'never'. error occured.

Playgroung Link

Related Issues:

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions