Skip to content

Instance properties declaration and initialization in constructor failed with useDefineForClassFields turned on #34942

Closed
@silentroach

Description

@silentroach

TypeScript Version: 3.7.2

Search Terms: useDefineForClassFields, constructor

Code

class Test {
    constructor(public readonly something: number) { }
}

const k = new Test(5);

Expected behavior: I expect k.something to be 5

Actual behavior: k.something is undefined, cause TS:

  1. writes the value to the property (as it was before)
  2. and then rewrite it (with writable: true, I think it is bug too) to void 0
class Test {
    constructor(something) {
        this.something = something;
        Object.defineProperty(this, "something", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
    }
}

Playground Link: https://www.typescriptlang.org/play/?useDefineForClassFields=true&target=7&ssl=3&ssc=2&pln=1&pc=1#code/MYGwhgzhAEAqCmEAu0DeAoaXrAPYDtkAnAV2CVyIAoAHEgIxAEthoj4wATAkAT2gi4AtvCQALJvgDmALmj4SQ+vCIBKNNAC+6TUA

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issueFixedA PR has been merged for this issueHigh Priority

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions