Skip to content

try-finally block can cause 'used before assigned` error #29684

Closed
@petebacondarwin

Description

@petebacondarwin

TypeScript Version: 3.3.1 - 3.4.0-dev.20190201 (inclusive)
This is not a problem in 3.3.0-rc

Search Terms:
used before being assigned, try finally

Code

let x: number;
x = Math.random();
let a: number;
try {
    if (x) {
        a = 1;
    } else {
        a = 2;
    }
} finally {
    console.log(x);
}

console.log(a); // <- error here

Expected behavior:

No error.

Actual behavior:

[ts] Variable 'a' is used before being assigned. [2454]
let a: number

Workaround:
You can avoid the error by apply the ! operator to the let statement:

let x !: number;

Playground Link:

Here is the playground link.

But this does not show the problem because the playground currently uses TS 3.2.x

Related Issues:

This looks similar to #12205. But that issue has not re-appeared and this issue only occurs if there is an if-else block in the try-finally block.

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