Skip to content

parameter initializer is bound to the wrong variable #22769

@ajafff

Description

@ajafff

TypeScript Version: 2.8.0-dev.20180321

Search Terms:

Code

let foo = 1;

(function (bar = foo) { // unexpected compiler error; works at runtime
    var foo = 2;
    return bar; // returns 1
})();

(function (bar = (baz = foo) => baz) { // unexpected compiler error; works at runtime
    var foo = 2;
    return bar(); // returns 1
})();

(function (bar = foo, foo = 2) { // correct compiler error, error at runtime
    return bar;
})();

Expected behavior:
No error on the first and second function expressions
This used to work in a previous release.

Actual behavior:
Compiler error on the first two function declarations even though the code works at runtime.

Initializer of parameter 'bar' cannot reference identifier 'foo' declared after it.

Variable 'foo' is used before being assigned.

Playground Link:

Related Issues:

Metadata

Metadata

Assignees

No one assigned

    Labels

    Effort: ModerateRequires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual".FixedA PR has been merged for this issueHelp WantedYou can do thisSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions