TS 5.4.5装饰器语法兼容问题:本地项目与Playground行为不一致
TypeScript 5.4.5 新装饰器语法本地不生效排查
问题现象
使用TypeScript 5.4.5版本时,新装饰器语法在TS Playground中可正常运行,但本地同配置项目中完全不生效;反之,旧版装饰器语法在本地项目中可正常工作。
新装饰器语法(TS Playground正常,本地无效)
class Test { @loggedMethod private async sayHi(txt: string) { console.log(txt); } async difMethod() { this.sayHi('well'); } } function loggedMethod(originalMethod: any, context: ClassMethodDecoratorContext) { const methodName = String(context.name); function replacementMethod(this: any, ...args: any[]) { console.log(`LOG: Entering method '${methodName}'.`); const result = originalMethod.call(this, ...args); console.log(`LOG: Exiting method '${methodName}'.`); return result; } return replacementMethod; } const tClass = new Test(); tClass.difMethod();
本地tsconfig.json配置
{ "compilerOptions": { "target": "es5", "lib": [ "dom", "dom.iterable", "esnext" ], // "experimentalDecorators": true, "suppressImplicitAnyIndexErrors": true, "allowJs": true, "skipLibCheck": true, "esModuleInterop": true, "allowSyntheticDefaultImports": true, "strict": true, "forceConsistentCasingInFileNames": true, "noFallthroughCasesInSwitch": true, "module": "esnext", "moduleResolution": "node", "resolveJsonModule": true, "isolatedModules": true, "noEmit": true, "jsx": "react-jsx" }, "include": [ "src" ] }
旧版装饰器语法(本地正常生效)
class Test { @loggedMethod private async sayHi(txt: string) { console.log(txt); } async difMethod() { this.sayHi('well'); } } function loggedMethod(target: any, name: string, descriptor: PropertyDescriptor) { target[name] = function replacementMethod(this: any, ...args: any[]) { console.log(`LOG: Entering method '${name}'.`); const result = descriptor.value?.call(this, ...args); console.log(`LOG: Exiting method '${name}'.`); return result; }; return target[name]; } const tClass = new Test(); tClass.difMethod();
排查情况
- 新旧语法核心差异在于装饰器函数的参数数量与类型:新语法接收
originalMethod和context两个参数,旧语法接收target、name、descriptor三个参数。 - 移除
"experimentalDecorators": true配置后,旧版语法会抛出符合TS5.x文档规则的错误:Unable to resolve signature of method decorator when called as an expression. The runtime will invoke the decorator with 2 arguments, but the decorator expects 3. - 无论是否开启
"experimentalDecorators": true,本地项目始终使用旧版装饰器逻辑。
请问还有哪些配置可能影响装饰器语法的生效逻辑?
内容的提问来源于stack exchange,提问作者Bastiat
相关产品推荐
相关产品推荐

