Open treblasft opened 10 months ago
It's not a crash if it successfully emits JavaScript. "Crash" means that tsc crashes. (I wish the issue template would make this more clear)
Thanks for the clarification, @jcalz. Updated the issue description!
@treblasft `class A { static propA = 'name'; static propB: string;
constructor(){ A.propB = A.propA }
// Making this method public makes transpiled JS code work as expected updateUser() { return A.propA; } }` you might consider initializing static propB within the constructor after the class declaration.
consider initializing static propB within the constructor after the class declaration.
Another workaround is to use this
instead of A
and then you also get inheritance:
export class A {
static propA = 'name';
static propB = this.propA;
// Making this method public makes transpiled JS code work as expected
#updateUser() {
return A.propA; // also consider using `this.constructor.propA` for inheritance, but it needs casting until https://github.com/microsoft/TypeScript/issues/3841 is fixed
}
}
š Search Terms
static property access private method transpiled JS
š Version & Regression Information
āÆ Playground Link
https://www.typescriptlang.org/play?target=9&moduleResolution=99&module=100&ts=5.2.2&noUnusedLocals=true&jsx=4&pretty=true&preserveWatchOutput=false&noErrorTruncation=true&isolatedModules=true&verbatimModuleSyntax=true&inlineSourceMap=true#code/KYDwDg9gTgLgBAYwDYEMDOa4EE4G8BQccaMKMAlgnGFBGDgLxwDkAdigLbDMDchxpClRp0AQnCZYAdCPp8iAegVEVqtXAB6WrXAAqATzDAAolFpQAXHADCKVqwjwowFABNqtI7HLBMEAGZwAK6srsD+5KzA7gAUzm6RAOYssljMAJT4-EpwALIoANZJcDAAFuSYXGUQ7mBBAEZIlHAchb4lUHZoYORI0XAAUgDKiDXAcADu0AVw6HCgRggw0fwAxEFgrmTAAKpowFAx6Xj8RM4wQVCs2DKeWPJwAL74z0A
š» Code
After updating to TypeScript 5.2.2, I encountered a TypeError when trying to access a static property on the same class. The compiled JavaScript code results in a reference to an uninitialized variable. This error seems to occur when a static member is referenced in a private method in the class.
š Actual behavior
The transpiled JavaScript results in a TypeError due to
_a
being used before it is assigned a value. The transpilation output incorrectly references_a
instead ofA
in the static property initialization and inside the private method.š Expected behavior
The static properties should be accessed the same way it does in the TypeScript code, as it is completely correct JavaScript code.
Additional information about the issue
Making the
#updateUser
method public makes TypeScript transpile JavaScript code as expected.