Avatar ·

编译TypeScript时的“Experimental decorators”警告

大家好!我刚开始学习TypeScript,遇到了一个不明白的问题。我写了一个带装饰器的简单类,但在编译时收到警告:“Experimental support for decorators is a feature that is subject to change in a future release. Set the 'experimentalDecorators' option in your 'tsconfig.json' or 'jsconfig.json' to remove this warning。” 这是我的代码: ```typescript function log(target: any, key: string, descriptor: PropertyDescriptor) { const original = descriptor.value; descriptor.value = function(...args: any[]) { console.log(`调用方法 ${key} 参数为:`, args); return original.apply(this, args); }; return descriptor; } class Calculator { @log add(a: number, b: number): number { return a + b; } } const calc = new Calculator(); calc.add(2, 3); ``` 我使用的是Visual Studio Code和TypeScript 5.3.2版本。在tsconfig.json文件中只有基本设置,没有添加任何关于装饰器的配置。