ITADN

decorators run in unexpected order

#566Opentrusktr 创建于 2025-11-22
T
trusktrcommented
This is unexpected and unintuitive: ```js 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](https://www.typescriptlang.org/play/?#code/GYVwdgxgLglg9mABAZxgczAQwDYAoD6AXIpmAJ4A0iECUApgB5TGlkCUiA3gFCLULI42OgDpscNLgDkAAVQYciACZ0aAJ0xQ4aqVRph6TEVgC2dNtwC+3bqEiwEiMybgEW5PbUbMS5Djz59QWExCWkZZzhlVW1NbV1+A29jTDMLa24IbExkZEQAWTIuXkQ5dCxsEkQAXkQARhKyhUqAIxrEACYbPgi6F0Q0OigUEBNcfxK+NSGQNSQoAAsYZBFMRABqREXlkRaS62sgA), [babel repl](https://babeljs.io/repl#?config_lz=N4IgZglgNgpgdgQwLYxALhAJxgBygOgCsBnEAGhB22JgBdS0BtRkeAN3NFoUwHM6GIACYwwCAK5R6ZAARwA9rRkQYMgIxrZCpSoD6SeQCN1a8iANDJMBmKg0Kh8b0gAPa-lqZxMAL4BdMhZibn4AWgBmTmEYAGN5TARaeOIANRhMYgh5OHQQACYABjzw0I0zHAgcGCgIOBgABUx5HHliBChcgAsEGIBrEH8AkGJ5cUwYmAAVAE8q3IsrAaA&code_lz=GYVwdgxgLglg9mABAZxgczAQwDYAoD6ANIhAlAKYAeUAlIgN4BQiJCyc25AdNnGrgHIAAqgw5EAE3KkATpihwZA4qTAVqXLAFtyNRgF9GjUJFgJEOrXAIqyVWg2asw7Tjz6ChluJOmL5isrO6lCamDp6howQ2JjIyIgAsgCejiwi6FjYiJiIALyIAIxOGWLZAEb5iABMRuneiGjkUCggWrh0TCwsMs0gMkhQABYwyFy5ANSIw6Nc5U6GhkA&evaluate=true&lineWrap=true&version=7.28.5) 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](https://github.com/tc39/proposal-decorators?tab=readme-ov-file#1-evaluating-decorators) 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 ```
1 条评论