Skip to content

Invalid error on computed property name using a variable with the same name as a constructor parameter of that class #42217

@ajafff

Description

@ajafff

TypeScript Version: 4.1.3

Search Terms:

Expected behavior:

No error. The downlevel emit is fine, so this won't give any unexpected behavior at runtime.

Actual behavior:

Initializer of instance member variable '[a]' cannot reference identifier 'a' declared in the constructor.

Code

const a = Symbol();
class C {
    [a] = 1;
    constructor(a) {}
}
Output
"use strict";
var _a;
const a = Symbol();
class C {
    constructor(a) {
        this[_a] = 1;
    }
}
_a = a;
Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "alwaysStrict": true,
    "esModuleInterop": true,
    "declaration": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "moduleResolution": 2,
    "target": "ES2017",
    "jsx": "React",
    "module": "ESNext"
  }
}

Playground Link: Provided

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: classesBehavior of various `class` constructs, e.g. mixins or base classes

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions