Skip to content

TDZ error is a bit overzealous #3656

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
Arnavion opened this issue Jun 27, 2015 · 3 comments
Closed

TDZ error is a bit overzealous #3656

Arnavion opened this issue Jun 27, 2015 · 3 comments
Labels
Bug A bug in TypeScript Duplicate An existing issue was already created

Comments

@Arnavion
Copy link
Contributor

function foo() {
    function bar() {
        x = 5;
    }
    let x;
    bar();
    console.log(x);
}

This complains that x = 5; is referencing x before its declaration. While technically true, this is not really a TDZ error. Though the access of x appears before its declaration in source code order, it's being executed after it, so the assignment will not throw a ReferenceError and is valid ES6 that will log 5

If it was bar(); let x; instead, then the error would be valid, since then the assignment would be invoked in x's TDZ.

But detecting this statically will require some brittle / expensive flow analysis, and it's not hard to reorder the code so it doesn't hit this, so maybe it's okay to leave it unfixed. The only real life scenario I can think of that would hit this problem is an existing codebase that prefers to put functions at the top of their respective scopes, that then starts migrating from var to let and const.

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Jun 28, 2015
@danquirk
Copy link
Member

#2854 is the larger item for flow control

@mhegazy
Copy link
Contributor

mhegazy commented Aug 11, 2015

looks like dupe of #4244

@mhegazy
Copy link
Contributor

mhegazy commented Aug 12, 2015

current proposal is to disable the error/warning if the declaration and reference are in different functions.

@mhegazy mhegazy added the Duplicate An existing issue was already created label Aug 12, 2015
@mhegazy mhegazy closed this as completed Aug 12, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants