Skip to content

Compiler bug with await inside async function with object spread #19187

Closed

Description

@sgoll

TypeScript Version: 2.5.2, 2.6.0-dev.20171014

async ({ foo, bar, ...rest }) => bar(await foo);

(Playground)

Expected behavior:

The resulting code should destructure the function's single argument, then do something—details of the function body are not relevant here as long as there is an await call inside another function call.

Actual behavior:

The compiler introduces a temporary variable _a that results in a runtime error: Cannot read property 'foo' of undefined.

The problem here is that the code above gets transpiled to the following (snipping prelude definitions of __awaiter, __generator, __rest etc.):

(function (_a) { return __awaiter(_this, void 0, void 0, function () {
    var foo = _a.foo, bar = _a.bar, rest = __rest(_a, ["foo", "bar"]);
    var _a;
    return __generator(this, function (_b) {
        // …
    });
}); });

The problem is the introduction of var _a in line 3 which shadows the function argument that should be destructured in line 2: order does not matter with var as long as the declarations are inside the same function, i.e. in line 2 the _a from line 3 gets scope precedence over the _a from line 1.

Metadata

Metadata

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions