Skip to content
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

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

Closed
silentroach opened this issue Nov 6, 2019 · 5 comments · Fixed by #34987
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue Fixed A PR has been merged for this issue High Priority

Comments

@silentroach
Copy link

silentroach commented Nov 6, 2019

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

@RyanCavanaugh
Copy link
Member

writable: true is intentional; readonly properties may be reassigned multiple times in the constructor body.

The order is definitely wrong though

@shayded-exe
Copy link

Yeah this is a massive issue. It makes useDefineForClassFields: true unusable.

@RizkyArifNur
Copy link

RizkyArifNur commented Nov 22, 2019

for now, you can avoid it by using this solution

class Test {
    readyonly something: number
    constructor(something: number) {
        this.something = something 
}
}

code above will fix the wrong ordering of definition

code playground: https://www.typescriptlang.org/play/?useDefineForClassFields=true&target=7&ssl=10&ssc=2&pln=5&pc=1#code/MYGwhgzhAEAqCmEAu0DeAoaXrAPYDtkAnAV2CVyIAoAHEgIxAEthoj4wATAkAT2gi4AtvCQALJvgDmALmj4SQ+vCIBKNNAC+6belCQYAQXy5xKhMjSYs7Lj36CR4yVICEchUpXWseQklJySipHUQlpD0VlNStsOOhnCAA6UOdpaABeAWEwlx8sbU0gA

@DanielRosenwasser DanielRosenwasser added the Fixed A PR has been merged for this issue label Nov 22, 2019
@michaeljota
Copy link

Hi @RyanCavanaugh, I found a bug, maybe related to this issue. When creating a class with a read-only property, the value is assigned with a variable with the same name as the property. However, this is an error in strict mode, because those variables are not defined.

https://www.typescriptlang.org/v2/en/play?useDefineForClassFields=true#code/MYGwhgzhAEAqCWAXEBTAyigTgN3sF0A3gFDTSYpgAmA9gHYgCe0iSqA-AFzQSKbx0A5gG5S0OmAC2KLjz4CRxMcHq9MAV2CIamABQVq9JtAAWlKlm5qFASiJiAvsQdA

You can confirm it happens only with read-only properties.

@sandersn
Copy link
Member

@michaeljota It looks like a bug. Here's a smaller repro:

// @useDefineForClassFields: true
class TitleService {
  readonly title?: string;
}

I filed #36910 to track it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue Fixed A PR has been merged for this issue High Priority
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants