-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
es2022 decorated class with static field initialization runtime error #48818
Comments
Sounds like a duplicate of #44908. But good news: Since the decorators proposal recently hit stage 3, we might get actual support for decorators in TypeScript in one of the next versions. |
sorry for the duplication
sorry for the duplication
yes it is a good news |
You probably want to set |
no, this doesn't solve the problem useDefineForClassFields: false it didn't solve the problem the 'static initialization blocks' is running before define |
let assume that an decorator 'logger' is applied on method function logger(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
const original = descriptor.value;
descriptor.value = function (...args: any) {
console.log('params: ', ...args);
const result = original.call(this, ...args);
console.log('result: ', result);
return result;
}
}
class Foo {
static instance = new Foo('instance');
static {
// logger is not applied yet
console.log('instance 4 + 6 = ', Foo.instance.add(4,6));
}
constructor(private name: string){}
@logger
add(x: number, y:number ) {
console.log('name', this.name);
return x + y;
}
}
const foo = new Foo('foo');
console.log('instance 4 + 1 = ', Foo.instance.add(4,1)); // run normal as expected, after apply the MethodDecorator
console.log('foo 1 + 2 = ', Foo.instance.add(1,2)); // run normal as expected, after apply the MethodDecorator |
Your playgrounds all have |
@fatcerberus That's a bug of the playground (applies to It's a visual issue. You can see in the sidebar that the target is not really ES3, because there are no classes in ES3. edit: Not sure why this confuses Andrew. :-D |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Itβs more of a sad face at the playground bug |
Bug Report
π Search Terms
decorated class with static field initialization runtime error
π Version & Regression Information
Typescript version 4.6.2
Target: ES2022
Module: ES2022
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
π Expected behavior
no runtime error and print 'break'
execute
static BREAK_INSTANCE = Object.freeze(new BreakStatement(BreakStatement.BreakSymbol));
after the decorator applied
OR
execute decorator definition in the first static initialization block
The text was updated successfully, but these errors were encountered: