Skip to content

Temporary variable declared too late #15306

Closed
@peblpebl

Description

@peblpebl

Code

var _b;
class C {
    static properties: { [key: string]: string } = {
        ["foo"] : "bar1",        
    };
    static otherproperties = (_b = {}, _b["foo"] = "bar2", _b);
}

// Use C
console.log(C.properties["foo"] + " " + C.otherproperties["foo"])
// End of original src

Expected behavior:

I would have expected that the sugar/javascript for constructing 'properties' would have declared the tmp variable before the construction. In the compile js the _a is declared after the last expression (but before that last comment??).

Google closure compiler warns about this.

Actual behavior:

var _b;
var C = (function () {
    function C() {
    }
    return C;
}());
C.properties = (_a = {},
    _a["foo"] = "bar1",
    _a);
C.otherproperties = (_b = {}, _b["foo"] = "bar2", _b);
// Use C
console.log(C.properties["foo"] + " " + C.otherproperties["foo"]);
var _a;
// End of original src

Activity

ahejlsberg

ahejlsberg commented on Apr 24, 2017

@ahejlsberg
Member

See #4138 and #7297.

peblpebl

peblpebl commented on Apr 24, 2017

@peblpebl
Author

This issue is better explained in #4138.

peblpebl

peblpebl commented on Apr 24, 2017

@peblpebl
Author

Hmm, this is not quite the same.
In #4138 and #7397 the declaration is in a function and if I understand ecma script correct - then valid everywhere within that function.

_a is not declared in a function here?

RyanCavanaugh

RyanCavanaugh commented on Apr 24, 2017

@RyanCavanaugh
Member

It doesn't matter. All var declarations are hoisted to the top of their enclosing lexical scope

mhegazy

mhegazy commented on May 8, 2017

@mhegazy
Contributor

This issue seems to have been already addressed or is unactionable at the moment. I am auto-closing it for now.

locked and limited conversation to collaborators on Jun 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @ahejlsberg@RyanCavanaugh@mhegazy@peblpebl

        Issue actions

          Temporary variable declared too late · Issue #15306 · microsoft/TypeScript