我刚刚从 github 克隆了 angular2-seed 项目并按照步骤操作,但我在 VS 代码中收到了这个警告 [ts] 对装饰器的实验性支持是一项在未来版本中可能会更改的功能。设置“实验装饰器”选项以删除此警告。
10 回答
在我创建的新服务上使用 @Injectable() 时遇到了这个问题。我收到了来自 VS Code 的打字稿编译器的警告。
当我将该新服务作为提供者添加到我打算使用它的模块时,警告消失了。
希望这可以解决您的问题。
转到vscode中的文件/首选项/用户设置,把这段代码
{
"typescript.tsdk": "node_modules/typescript/lib"
}
在您的tsconfig.json
文件中,将项目的路径添加到include
属性中,如下所示:
"include": [
"app/**/*"
]
或者,您可以将单个文件添加到files
属性中。以下是我tsconfig.json
的示例(对于 Angular2 应用程序):
{
"compileOnSave": false,
"buildOnSave": false,
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"noEmitHelpers": true,
"sourceMap": true,
"types": [
"node"
],
"typeRoots": [
"../node_modules/@types"
],
"lib": [
"es2015",
"dom"
]
},
"files": [
"app/main.ts",
"app/app.module.ts"
],
"include": [
"app/**/*.ts"
],
"exclude": [
"node_modules",
"dist",
"build",
"app/main.aot.ts"
]
}
你的 tsconfig.json 应该是这样的:
{
"compilerOptions": {
"experimentalDecorators": true
},
"files": [], //add a "files" array (in my case empty)
"exclude": [
"node_modules"
]
}
或者,您可以在 VSCode 首选项(用户设置)中添加一行
"javascript.implicitProjectConfig.experimentalDecorators": true
在 VS Code 设置中找到“experimentalDecorators”。这将列出 Extension->TypeScript 与属性 JavaScript-> Implicit Project Config -> Experimental Decorators 启用此设置将解决警告
在 VS Code 中,打开settings.json
并添加以下行:
"javascript.implicitProjectConfig.experimentalDecorators": true
我使用这样的命令解决了类似的问题
tsc .\app.ts --experimentalDecorators --target es6
其中 .\app.ts 是文件的路径
我在新添加的类的 VS Code 中收到此警告。项目编译没有任何错误。当我开始通过import
警告引用新添加的类时,就会出现。
在这种情况下,无需更改 VS Code 或 Typescript 中的配置。
这是由于使用了旧版本的打字稿。您必须升级您的 typescript 版本才能删除此消息。
npm install -g typescript@latest
此外,您还需要将您的 npm 升级到最新版本。