How do I use TypeScript 1.6 with Visual Studio Cod

2020-02-29 01:41发布

问题:

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:

Generators are only available when targeting ECMAScript 6

But my tsconfig.json does have the ES6 target:

{
    "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.

Generators are not currently supported.

How can I get TypeScript and generators to work properly together in VS Code?

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.:

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

回答1:

I know now!

1. IntelliSense

You can use the typescript.tsdk setting to point VSCode to TypeScript binaries. Upgrade your TypeScript to 1.6 and set the location properly.

You can do it either in your user/workspace settings, or per project in the .vscode/settings.json file. OS X example:

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

2. Compiler

You also need to make sure your .vscode/tasks.json points to the new binary and makes the compiler operate in Explicit project mode, i.e. use tsconfig.json instead of taking a list of files to compile as an argument.

{
    "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):

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

Restart the editor afterwards!



回答2:

You can change your user settings in VS Code and set "typescript.tsdk" to a custom location .

If you install the nightly (npm install -g typescript@next), you can point to that version of TypeScript's lib folder.

More

Reasons and setup instructions for using ts latest are covered here : https://basarat.gitbooks.io/typescript/content/docs/getting-started.html#typescript-version