proposal-decorators
proposal-decorators copied to clipboard
decorators run in unexpected order
This is unexpected and unintuitive:
function signal(_, context) {
console.log('@signal decorator', context.name)
}
function memo(_, context) {
console.log('@memo decorator', context.name)
}
class My {
@signal a = 1
@signal b = 2
@memo get sum() {
return this.a + this.b
}
}
typescript playground, babel repl
Output:
@memo decorator sum
@signal decorator a
@signal decorator b
Every time I want to do something extremely simple, like rely on ordering of definitions so I can simply initialize storage in that order, I can't.
Decorators always want me to go to extravagant lengths to achieve otherwise simple concepts.
Or is this a bug in both TypeScript and Babel?
The proposal-decorators README here says this:
Decorators are evaluated as expressions, being ordered along with computed property names. This goes left to right, top to bottom.
With or without that description, I'd expect this output:
@signal decorator a
@signal decorator b
@memo decorator sum