【问题标题】:How do I use TypeScript 1.6 with Visual Studio Code to get generators support?如何使用 TypeScript 1.6 和 Visual Studio Code 来获得生成器支持?
【发布时间】:2016-08-10 19:09:30
【问题描述】:

我在 Visual Studio Code 中针对 ES6 有一段时间了,但是当我尝试切换到 TypeScript 时,它会抛出以下错误:

生成器仅在面向 ECMAScript 6 时可用

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

{
    "compilerOptions": {
        "target": "ES6",
        "module": "amd",
        "sourceMap": true
    }
}

所以我尝试了npm install -g typescript@1.6.0-beta,但看起来 VSCode 不在乎。

目前不支持生成器。

如何让 TypeScript 和生成器在 VS Code 中正常协同工作?

更新

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"

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

【问题讨论】:

  • 如果你弄明白了,请分享!我也很感兴趣
  • @bali182 完成 :) 祝你好运!
  • 哇,谢谢,看看这个,它可能值得等到它的测试版和生产版 1.6 发布 :)

标签: node.js typescript ecmascript-6 visual-studio-code


【解决方案1】:

我现在知道了!

1。智能感知

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

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

"typescript.tsdk": "/usr/local/lib/node_modules/typescript/lib"

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"
}

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

{
    "compilerOptions": {
        "target": "ES6", //The key, of course.
        "module": "amd",
        "sourceMap": true
    },
    "exclude": [
        "node_modules",
        ".vscode"
    ]
}

之后重启编辑器!

【讨论】:

  • 这在 typescript 1.7-dev 中工作,在 1.6 中它将无法编译
  • 在我的情况下,我需要运行本地安装的 tsc 编译器作为 npm 包。这是对我有用的.vscode/tasks.json: { "version": "0.1.0", "command": "${cwd}/node_modules/.bin/tsc", "isShellCommand": true, "showOutput": "silent ", "args": [], "problemMatcher": "$tsc" }
【解决方案2】:

您可以在 VS Code 中更改用户设置并将 "typescript.tsdk" 设置为自定义位置。

如果您安装 nightly (npm install -g typescript@next),您可以指向该版本的 TypeScript 的 lib 文件夹。

更多

此处介绍了使用 ts latest 的原因和设置说明:https://basarat.gitbooks.io/typescript/content/docs/getting-started.html#typescript-version

【讨论】:

  • 感谢您的帮助。我实际上已经尝试过了,甚至将其作为答案发布,但将其删除,因为即使 IntelliSense 错误消失了,CMD+Shift+B 仍然会喷出app.ts(112,20): error TS1220: Generators are only available when targeting ECMAScript 6 or higher.,尽管将 tasks.json 指向相同的二进制文件。
  • @basarat 这实际上是现在的 lib 文件夹。 bin 文件夹不起作用。
  • @Schotime 是的。是错别字
猜你喜欢
  • 2017-03-26
  • 1970-01-01
  • 2020-11-12
  • 1970-01-01
  • 1970-01-01
  • 2017-05-15
  • 2018-05-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多