question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

order of applied class/constr-param decorators different than in tsc

See original GitHub issue

When I compile

@ClassDec()
export class Decoratee {
  constructor(@ParamDec() param: string) {}
}

with tsc I end up with (I replaced some tslib calls with what they will be replaced with):

let Decoratee = class Decoratee {
    constructor(param) { }
};
Decoratee = tslib_1.__decorate([
    ClassDec(),
    ParamDec(),
    Reflect.metadata("design:paramtypes", [String])
], TokenService);

tslib then will call these three decorators on Decoratee, starting with the last one (Reflect.metadata), then alls ParamDec, and finally ClassDec`.

Using this plugin, I get something like this, with the order being differently:

var _dec, _dec2, _class;
var Decoratee = (
  _dec = (0, ClassDec)(),
  _dec2 = Reflect.metadata("design:paramtypes", [String]),
  _dec(
    _class = _dec2(
      _class = function Decoratee(param) {}
    ) || _class
  ) || _class
);
(0, ParamDec)()(Decoratee, undefined, 0);

Compiling this with babel will in some cases have different results, if libraries/frameworks rely on a consistent order. I do not know, if the order is defined in the metadata proposal, did not find anything so far. I think you mentioned this issue also at the bottom of README.md. Specifically in the Angular DI, the @Injectable decorator expects all @Injects inside the constructor parameter to already have been applied.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
leonardfactorycommented, Dec 12, 2019

Awesome! I’d like to see the output code, and we should add some tests. Anyway, if you can build up something with a PR it would be cool 👍

1reaction
wthocommented, Dec 11, 2019

By the way, this also resolves #22, I guess because we now put the new decorator on the class itself and this in return leads to the Reflect.metadata decorator being created.

Read more comments on GitHub >

github_iconTop Results From Across the Web

babel-plugin-transform-typescript-metadata - GithubHelp
Babel plugin to emit decorator metadata like typescript compiler from GithubHelp. ... order of applied class/constr-param decorators different than in tsc.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found