简体   繁体   English

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

[英]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和生成器一起正常工作?

Update 更新

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! 我现在知道了!

1. IntelliSense 1.智能感知

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"

2. Compiler 2.编译器

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文件夹。

More 更多

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.

相关问题 如何在没有将目标设置为ES6的情况下获得打字稿中的生成器支持? - How to get support of generators in typescript without setting target to ES6? 如何在节点代码中使用Sails命令行生成器? - How do I use sails command line generators in my node code? 如何让我的 node.js 与我的 Visual Studio 代码一起使用? - How do I get my node.js to work with my visual studio code? 如何在 Visual Studio Code 中的事件上使用 JSDoc 自定义 EventEmitter? - How do I JSDoc custom EventEmitter on events in Visual Studio Code? 如何在 Visual Studio Code 中运行 npm 命令? - How do I run npm command in Visual Studio Code? 在 MacOS-catalina 上的 Visual Studio Code 中获取 Typescript 的问题 - Problem to get Typescript working in Visual Studio Code on MacOS-catalina 打字稿:Visual Studio代码无法获取正确的mssql类型 - typescript: visual studio code can't get the correct typings for mssql 如何获得Visual Studio Typescript 1.7智能感知以使用Node.js库(Angular2,rxjs)? - How can I get Visual Studio Typescript 1.7 intellisense to work with nodejs libraries (angular2, rxjs)? 如何在Visual Studio Code中调试TypeScript Express应用 - How to debug TypeScript Express app in Visual Studio Code 如何在Visual Studio代码中使用Typescript的多个文件? - How to work with multiple files of Typescript in Visual studio code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM