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

Class property decorators gets applied before the class decorator itself #77

Open
marcus-sa opened this issue Apr 17, 2018 · 4 comments

Comments

@marcus-sa
Copy link

marcus-sa commented Apr 17, 2018

Decorators doesn't get resolved in the order that they're being applied.

For instance, if we take this class:

@Module()
export class Server {

  @Lifecycle.subscribe()
  create() {
    // ...
  }

  @Lifecycle.subscribe()
  start() {
    // ...
  }

}

it get's transpiled to:

// ...

let Server = class Server {
    create() {
    }
    start() {
    }
};
__decorate([
    Lifecycle_1.Lifecycle.subscribe(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", void 0)
], Server.prototype, "create", null);
__decorate([
    Lifecycle_1.Lifecycle.subscribe(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", void 0)
], Server.prototype, "start", null);
Server = __decorate([
    Module_1.Module()
], Server);
exports.Server = Server;

As you can see in the upper section of code, I defined the Module decorator before the others, but weird enough it gets transpiled as the last in the order.
It shouldn't be this way.

Currently I don't think that there's a workaround for me, as it depends on being defined first because it binds the class to a dependency injection container.

@loganfsmyth
Copy link
Owner

This example appears to be from Typescript, not the result of this Babel transform.

@loganfsmyth
Copy link
Owner

That said, it seems the workaround could be

@Module()
export class Server {

changing to be

const modDec = Module();
@modDec
export class Server {

@marcus-sa
Copy link
Author

Thanks I'll try it out.
Would it work if I disabled experimentalDecorators in my tsconfig.json and started using this combined with Babel and TypeScript?

@loganfsmyth
Copy link
Owner

I don't really know anything about TS, so I don't think I can give good advice on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants