Skip to content

Throw in catch causes type narrowing in finally block  #34797

Closed
@markusjohnsson

Description

@markusjohnsson

TypeScript Version: 3.7.x-dev.201xxxxx
3.6,
3.7-beta

Search Terms:
try catch finally never

Code

function f() {
    let a: number | null = null;
    try {
        a = 123;
        return a;
    }
    catch (e) {
        throw e;  // remove line and error disappears
    }
    finally {
        console.log(a); // works fine, logs 123
        if (a != null && a.toFixed(0) == "123") { 
             // Property 'toFixed' does not exist on type 'never'.(2339)
        }
    }
}

f();

Expected behavior:
a should have type number | null in the finally block.

Actual behavior:
a has type null in the finally block and narrowing with a != null it becoms never

Playground Link:
http://www.typescriptlang.org/play/index.html?ts=3.7-Beta&ssl=1&ssc=1&pln=20&pc=5#code/GYVwdgxgLglg9mABMAFASkQbwFCL4gGwFMpEBDALkTBAFsAjIgJ0QB9qQCDEBeDrgNzZc+KEwCeWEfnxleiAIwAmAMxCZMpiRBMkZdfgC+0xBDJQIAC0QoiGHBtGWmcAO6IiAxCeMzgMMDIuSRwTGQgEAGc4YgA6AjgAcxQyNANHGGAbOQBCPhouRAAyIvJYqDgAMRgADyIAExQABgwePgAiZRV2+zDHb37EXyNsY2xUNKA

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