Skip to content

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

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
petebacondarwin opened this issue Feb 1, 2019 · 9 comments
Closed

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

petebacondarwin opened this issue Feb 1, 2019 · 9 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@petebacondarwin
Copy link
Contributor

petebacondarwin commented Feb 1, 2019

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.

@j-oliveras
Copy link
Contributor

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

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Feb 5, 2019
@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript and removed Needs Investigation This issue needs a team member to investigate its status. labels Feb 6, 2019
@DanielRosenwasser
Copy link
Member

@weswigham can you port this into release-3.3?

@DanielRosenwasser
Copy link
Member

This should be available in TypeScript 3.3.3333.

@DanielRosenwasser DanielRosenwasser added the Fixed A PR has been merged for this issue label Feb 22, 2019
@AviVahl
Copy link

AviVahl commented Feb 22, 2019

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

@xaviergonz
Copy link

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

@tommck
Copy link

tommck commented Feb 26, 2019

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

@Gerst20051
Copy link

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

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Feb 27, 2019

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

@tommck
Copy link

tommck commented Feb 27, 2019

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 :)

@microsoft microsoft locked as resolved and limited conversation to collaborators Feb 27, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

9 participants