Skip to content
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

should warn/error on forward references during field initialization #12673

Closed
Spongman opened this issue Dec 5, 2016 · 4 comments
Closed

should warn/error on forward references during field initialization #12673

Spongman opened this issue Dec 5, 2016 · 4 comments
Assignees
Labels
Committed The team has roadmapped this issue Fixed A PR has been merged for this issue Suggestion An idea for TypeScript

Comments

@Spongman
Copy link

Spongman commented Dec 5, 2016

TypeScript Version: 2.0.10

class Test
{
    _b = this._a; // undefined, no error/warning
    _a = 3;

    static _B = Test._A; // undefined, no error/warning
    static _A = 3;
    
    method()
    {
        let a = b; // Block-scoped variable 'b' used before its declaration
        let b = 3;
    }
}

I'd expect the forward field references to trigger an error (or at least a warning, maybe configurable) similar to function locals.

@mhegazy mhegazy added the Bug A bug in TypeScript label Dec 5, 2016
@mhegazy mhegazy added this to the TypeScript 2.2 milestone Dec 5, 2016
@mhegazy mhegazy added Suggestion An idea for TypeScript and removed Bug A bug in TypeScript labels Dec 5, 2016
@RyanCavanaugh RyanCavanaugh added the In Discussion Not yet reached consensus label Dec 5, 2016
@SlurpTheo
Copy link

SlurpTheo commented Dec 15, 2016

TypeScript Version: 2.1.4
I'm not certain if this is the same issue/bug and I understand how things gets convoluted quickly when thinking about function declarations and how they hoist within the function where they are defined, but I was recently surprised by the behaviour of a function expression accessing a block-scoped "outer/free" variable (e.g., let x) before its declaration.

Code

function B() {
    // comment-out the '**A' lines to see the 'used before its declaration' error

    ;(function() {   // **A
        ++x;
    })()             // **A

    let x = 0
}

Expected behavior:
A compile error both with and without the nested function expression.

Actual behavior:
Use of a block-scoped, let variable before its declaration.

@SlurpTheo
Copy link

> (()=>{ (()=>x)(); let x; })()
ReferenceError: x is not defined
    at repl:1:13
    at repl:1:15
    at repl:1:28
    at sigintHandlersWrap (vm.js:22:35)
    at sigintHandlersWrap (vm.js:96:12)
    at ContextifyScript.Script.runInThisContext (vm.js:21:12)
    at REPLServer.defaultEval (repl.js:313:29)
    at bound (domain.js:280:14)
    at REPLServer.runBound [as eval] (domain.js:293:12)
    at REPLServer.<anonymous> (repl.js:513:10)
> (()=>{ let x; (()=>x)(); })()
undefined
> process.version
'v6.9.2'
>

@RyanCavanaugh
Copy link
Member

@SlurpTheo I think you're basically looking for #11498

@SlurpTheo
Copy link

@RyanCavanaugh I think that is very close to my concern -- yes/thanks!

(My 2nd #12673 (comment) from earlier today shows how --target es5 will generate code that will behave/error differently from code output via --target es2015)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Committed The team has roadmapped this issue Fixed A PR has been merged for this issue Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants