Typescript: try-finally block can cause 'used before assigned` error

Created on 1 Feb 2019  路  9Comments  路  Source: microsoft/TypeScript


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 https://github.com/Microsoft/TypeScript/issues/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.

Bug Fixed

Most helpful comment

This should be available in TypeScript 3.3.3333.

All 9 comments

PR #29466 solved this but it seems that the change are lost.

The only versions that it works are 3.3.0-dev.20190118 and the 3.3.0-rc.

Starting 3.3.0-dev.20190119 the PR changes are lost. I only test 3.3.0-dev.20190119, 3.3.0-dev.20190123 (made hours after the 3.3.0-rc), 3.3.0-dev.20190125, 3.4.0-dev.20190129 and 3.4.0-dev.20190201.

// CC @weswigham

@weswigham can you port this into release-3.3?

This should be available in TypeScript 3.3.3333.

I thought the 3333 was a typo. :roll_eyes:
New version number is inconsistent with how this project versioned its past stable releases.
Why not simply 3.3.4?

I think that's too late since 3.3.3333 > 3.3.4 by (3333 - 4) = 3329 patch versions :/

yeah.. really strange version number.. Would be good to hear a reason for it

Is there a funny reason for the patch version to be 3333 instead of 4? @DanielRosenwasser @weswigham

It was simply for the repeating 3s. Sorry for any confusion, though otherwise we don't expect that it'll adversely affect anything else.

it's pretty weird that we can't have 3.3.4 now :) next version will be 3.3.3334 if it's a patch :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jbondc picture jbondc  路  3Comments

Antony-Jones picture Antony-Jones  路  3Comments

zhuravlikjb picture zhuravlikjb  路  3Comments

MartynasZilinskas picture MartynasZilinskas  路  3Comments

CyrusNajmabadi picture CyrusNajmabadi  路  3Comments