Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
s2terminal opened this issue Dec 12, 2019 · 0 comments · Fixed by #35730
Closed
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@s2terminal
Copy link

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:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants