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

preset-typescript + decorator on abstract base class overwrites members with undefined #10514

Open
justinwilaby opened this issue Oct 1, 2019 · 4 comments

Comments

@justinwilaby
Copy link

commented Oct 1, 2019

Bug Report

Current Behavior
Decorating an abstract base class causes implementors to have undefined function refs even though the function is implemented.

Given this scenario:

abstract class A {

  @decorate()
  private decoratedMember: boolean

  protected abstract myMethod(): void
}

class B extends A {
  private config: any;

  constructor(config: any) {
    super();
    this.config = config;
  }

  public runMyMethod(): void {
    this.myMethod(); // <--- Error: undefined is not a function
  }

  protected myMethod(): void {
   // Do stuff
  }
}

const b = new B();
b.runMyMethod();

Expected behavior/code
Base class definitions should not initialize abstract methods to void 0 when decorators are used. This will allow implementors of abstract classes that use decorators to properly define concrete functions.

**Babel Configuration **
.babelrc

{
  "presets": [
    [
      "@babel/preset-env",
      {
        "targets": {
          "electron": "6"
        }
      }
    ],
    "@babel/preset-typescript"
  ],
  "ignore": ["**/*.d.ts"],
  "plugins": [
    "@babel/plugin-transform-react-jsx",
    [
      "@babel/plugin-proposal-decorators",
      {
        "decoratorsBeforeExport": true
      }
    ],
    "@babel/plugin-proposal-class-properties",
    "@babel/plugin-transform-runtime"
  ]
}

Environment

  • Babel version(s): 7.5.5
  • Node/npm version: 12.7
  • OS: OSX 10.14.6
  • Monorepo: lerna
  • How you are using Babel: cli
@babel-bot

This comment has been minimized.

Copy link
Collaborator

commented Oct 1, 2019

Hey @justinwilaby! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite."

@nicolo-ribaudo

This comment has been minimized.

Copy link
Member

commented Oct 2, 2019

Note that TypeScript 3.7 will align with Babel's behaviour of initializing class fields to undefined.

@justinwilaby

This comment has been minimized.

Copy link
Author

commented Oct 3, 2019

@nicolo-ribaudo - This defect is not related to class fields but rather class functions.

If I define a base class ( class A) with an empty abstract method and then decorate any class field, all classes that extends the base (class B) will have undefined for the abstract method even when that method is implemented by the subclass. If I remove the decorator, everything works as expected.

It seems the _decorate helper is overwriting the subclass prototype.

@nicolo-ribaudo

This comment has been minimized.

Copy link
Member

commented Oct 3, 2019

Oh I initially misunderstood the bug 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.