Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Class method decorators reset the first field. #61665

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

Open
xiaoxiyao opened this issue May 6, 2025 · 1 comment
Open

Class method decorators reset the first field. #61665

xiaoxiyao opened this issue May 6, 2025 · 1 comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@xiaoxiyao
Copy link

🔎 Search Terms

"__runInitializers", "decorator"

Possibly related issues:#54264

🕗 Version & Regression Information

  • I was unable to test this on prior versions because non-experimental decorators are only available in 5.0+

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/GYVwdgxgLglg9mABAIzuAJgCjgJxgcxjAEMAbAWQFMoALOdALkWLAE8AaRCBKSgDyhMAwqWIBnMVVr0AIpW45iUXEJ78oASiYA3ODHSIA3gChEXBGKiIAttTroAcsVuIAvObC8BAOhK2A3KYeXlDexOjoAJJgMLBkMABelDiYoJCwCIiYtDBiTCysGkZBZjliANq20o7OlAC6bohllXb0TrZ13shEWGUagWYAvv3Gg8bGEKISiABC4pTFZtxgljgg0LiYRSZmpTS53kSxWwOIY0EADjhwvNCUBkdQW8VBY+eT4mKIAGJwcIjqShgdBfOZiBY7FDEHAAfiYqyI+ECQTg2mSeHQC0ez0hZjEIAuyUOMSeI12TX2Yi60MaAHIaJRSKQ4LTTuczAABVAYS4gZCkGAQRDoOAAZTgVX2YHwOPGQ1GEwscFIlG8zJlYEoAHcfn8ttScP0gA

💻 Code

function bound(originalMethod: any, context: ClassMethodDecoratorContext): void {
  const methodName = context.name;
  context.addInitializer(function (this: any) {
    this[methodName] = this[methodName].bind(this);
  });
}

class Base {
  constructor() {
    this.init();
  }

  protected init() {

  }
}

class Foo extends Base {
  bar?: string;

  override init() {
    super.init();
    this.bar = 'hello';
  }

  @bound
  public doSomething() {

  }
}
console.log(new Foo().bar);

🙁 Actual behavior

Prints undefined

🙂 Expected behavior

Prints hello

Additional information about the issue

The generated JS is as follows:

constructor() {
      super(...arguments);
      this.bar = __runInitializers(this, _instanceExtraInitializers);
}

Here, it always assigns to the first field, which should be a bug.

@MartinJohns
Copy link
Contributor

This is working as intended and independent of using decorators. You have the same result with this code:

class Base {
  constructor() {
    this.init();
  }

  protected init() {

  }
}

class Foo extends Base {
  bar?: string;

  override init() {
    super.init();
    this.bar = 'hello';
  }
}
console.log(new Foo().bar);

This results in "hello" being printed when useDefineForClassFields is turned off, and undefined when it's turned on. Using the decorators requires useDefineForClassFields to be turned on.

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label May 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants