[英]How do I use TypeScript 1.6 with Visual Studio Code to get generators support?
I've been targeting ES6 for a while in Visual Studio Code, but when I try to switch to TypeScript, it throws errors such as: 我在Visual Studio Code中已经将ES6定位了一段时间,但是当我尝试切换到TypeScript时,它会抛出错误,例如:
Generators are only available when targeting ECMAScript 6 生成器仅在定位ECMAScript 6时可用
But my tsconfig.json does have the ES6 target: 但我的tsconfig.json确实有ES6目标:
{
"compilerOptions": {
"target": "ES6",
"module": "amd",
"sourceMap": true
}
}
So I tried npm install -g typescript@1.6.0-beta
but it looks like VSCode doesn't care. 所以我尝试了npm install -g typescript@1.6.0-beta
typescript@1.6.0-beta,但看起来VSCode并不关心。
Generators are not currently supported. 目前不支持生成器。
How can I get TypeScript and generators to work properly together in VS Code? 如何在VS Code中使TypeScript和生成器一起正常工作?
Changing typescript.tsdk
to the 1.6 binary seems to fix IntelliSense errors, but this tasks.json still prints out error TS1220: Generators are only available when targeting ECMAScript 6 or higher.
更改typescript.tsdk
到1.6二进制似乎解决智能感知错误,但是这仍然tasks.json打印出error TS1220: Generators are only available when targeting ECMAScript 6 or higher.
: :
"version": "0.1.0",
"command": "/usr/local/lib/node_modules/typescript/bin/tsc",
"showOutput": "silent",
"windows": {"command": "tsc.exe"},
"isShellCommand": true,
"args": ["app.ts"],
"problemMatcher": "$tsc"
However, /usr/local/lib/node_modules/typescript/bin/tsc --target ES6 app.ts
used manually in the terminal does work. 但是, /usr/local/lib/node_modules/typescript/bin/tsc --target ES6 app.ts
终端中手动使用的/usr/local/lib/node_modules/typescript/bin/tsc --target ES6 app.ts
可以正常工作。
I know now! 我现在知道了!
You can use the typescript.tsdk
setting to point VSCode to TypeScript binaries. 您可以使用typescript.tsdk
设置为指向VSCode以打字稿二进制文件。 Upgrade your TypeScript to 1.6 and set the location properly. 将TypeScript升级到1.6并正确设置位置。
You can do it either in your user/workspace settings, or per project in the .vscode/settings.json file. 您可以在用户/工作区设置中执行此操作,也可以在.vscode / settings.json文件中的每个项目中执行此操作。 OS X example: OS X示例:
"typescript.tsdk": "/usr/local/lib/node_modules/typescript/lib"
You also need to make sure your .vscode/tasks.json points to the new binary and makes the compiler operate in Explicit project mode , ie use tsconfig.json instead of taking a list of files to compile as an argument. 您还需要确保.vscode / tasks.json指向新的二进制文件并使编译器在显式项目模式下运行 ,即使用tsconfig.json而不是将文件列表作为参数进行编译。
{
"version": "0.1.0",
"command": "/usr/local/lib/node_modules/typescript/bin/tsc",
"showOutput": "silent",
"windows": {"command": "tsc.exe"},
"isShellCommand": true,
"args": [], //do not pass any files to the compiler. This way it will use tsconfig.json where you can set target: "ES6"
"problemMatcher": "$tsc"
}
And finally tsconfig.json (in the project's root directory): 最后是tsconfig.json (在项目的根目录下):
{
"compilerOptions": {
"target": "ES6", //The key, of course.
"module": "amd",
"sourceMap": true
},
"exclude": [
"node_modules",
".vscode"
]
}
Restart the editor afterwards! 之后重启编辑器!
You can change your user settings in VS Code and set "typescript.tsdk"
to a custom location . 您可以在VS Code中更改用户设置,并将"typescript.tsdk"
设置为自定义位置。
If you install the nightly ( npm install -g typescript@next
), you can point to that version of TypeScript's lib
folder. 如果您每晚安装( npm install -g typescript@next
),您可以指向该版本的TypeScript的lib
文件夹。
Reasons and setup instructions for using ts latest are covered here : https://basarat.gitbooks.io/typescript/content/docs/getting-started.html#typescript-version 使用ts最新的原因和设置说明如下: https : //basarat.gitbooks.io/typescript/content/docs/getting-started.html#typescript-version
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.