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:
- writes the value to the property (as it was before)
- 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