如何使用TypeScript 1.6和Visual Studio Code来获得生成器支持?

Hai*_*der 5 node.js typescript ecmascript-6 visual-studio-code

我在Visual Studio Code中已经将ES6定位了一段时间,但是当我尝试切换到TypeScript时,它会抛出错误,例如:

生成器仅在定位ECMAScript 6时可用

但我的tsconfig.json确实有ES6目标:

{
    "compilerOptions": {
        "target": "ES6",
        "module": "amd",
        "sourceMap": true
    }
}
Run Code Online (Sandbox Code Playgroud)

所以我试过npm install -g typescript@1.6.0-beta但看起来VSCode并不关心.

目前不支持生成器.

如何在VS Code中使TypeScript和生成器一起正常工作?

更新

更改typescript.tsdk为1.6二进制文件似乎修复了IntelliSense错误,但是这个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"
Run Code Online (Sandbox Code Playgroud)

但是,/usr/local/lib/node_modules/typescript/bin/tsc --target ES6 app.ts在终端中手动使用确实有效.

Hai*_*der 10

我现在知道了!

1.智能感知

您可以使用该typescript.tsdk设置将VSCode指向TypeScript二进制文件.将TypeScript升级到1.6并正确设置位置.

您可以在用户/工作区设置中执行此操作,也可以在.vscode/settings.json文件中的每个项目中执行操作.OS X示例:

"typescript.tsdk": "/usr/local/lib/node_modules/typescript/lib"
Run Code Online (Sandbox Code Playgroud)

2.编译器

您还需要确保.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"
}
Run Code Online (Sandbox Code Playgroud)

最后是tsconfig.json(在项目的根目录下):

{
    "compilerOptions": {
        "target": "ES6", //The key, of course.
        "module": "amd",
        "sourceMap": true
    },
    "exclude": [
        "node_modules",
        ".vscode"
    ]
}
Run Code Online (Sandbox Code Playgroud)

之后重启编辑器!

  • 在我的情况下,我需要运行本地安装的tsc编译器作为npm包.这是为我工作的`.vscode/tasks.json`:{"version":"0.1.0","command":"$ {cwd} /node_modules/.bin/tsc","isShellCommand":true," showOutput":"silent","args":[],"problemMatcher":"$ tsc"} (3认同)