Skip to content

Invalid emit for class expression with computed property name in parameter initializer #29159

Closed
@ajafff

Description

@ajafff

TypeScript Version: 3.2.1

Search Terms:

Code

// @target: ES6
function test(
    x = class {
        [y] = 1
    },
    y: 'foo',
) {}

Expected behavior:

Calling test(undefined, "foo") executes without an error.

Actual behavior:

Runtime error ReferenceError: _b is not defined.

The helper variables _a and _b are declared inside the function body, but parameter initializers cannot reference declarations in the function body. I guess this could be fixed by wrapping the emit of the class expression in an IIFE.

"use strict";
function test(x = (_b = class {
        constructor() {
            this[_a] = 1;
        }
    },
    _a = y,
    _b), y) { var _a, _b; }

Note: The same code downleveled to ES5 works as expected as the parameter initializer is emitted inside the function body.

Playground Link: https://agentcooper.github.io/typescript-play/?target=2#code/GYVwdgxgLglg9mABFApgZygCgFCL4gD0QF5EIAbAQzTUQG9d8mBtATwF0TEBGRvAXwA0fRKwBciAOTA4cScICU9ftiA

Related Issues:

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions