Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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 [email protected] 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.

like image 279
Haider Avatar asked Sep 03 '15 15:09

Haider


1 Answers

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!

like image 145
Haider Avatar answered Sep 27 '22 20:09

Haider