Skip to content

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

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

Closed
ajafff opened this issue Dec 26, 2018 · 1 comment
Labels
Bug A bug in TypeScript
Milestone

Comments

@ajafff
Copy link
Contributor

ajafff commented Dec 26, 2018

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:

@weswigham weswigham added the Bug A bug in TypeScript label Dec 27, 2018
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Mar 14, 2019
@rbuckton
Copy link
Contributor

The example is invalid in ES2015 and up as each parameter initializer essentially gets its own lexical scope and cannot reference other parameters or variables in the body. At some point we added an error to this effect:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants