1

I have the following TypeScript

var foo = (...myParams) => {
    for (var i = 0; i < myParams.length; i++){
        console.log(myParams[i] + " ");
    }
};

foo('a', 'b', 'c');

However, when it's compiled by WebStorm finds errors in the compiled code:

WebStorm errors

and when it's rune by Node, Node doesn't recognise the rest expression.

C:\PluralSight\TypeScript>node rest-parameter.js
C:\PluralSight\TypeScript\rest-parameter.js:5
var foo = (...myParams) => {
           ^^^

SyntaxError: Unexpected token ...
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:414:25)
    at Object.Module._extensions..js (module.js:442:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:311:12)
    at Function.Module.runMain (module.js:467:10)
    at startup (node.js:136:18)
    at node.js:963:3
6
  • kangax.github.io/compat-table/es6 Commented Apr 18, 2016 at 13:23
  • 1
    @squint Cool link. Node just doesn't support them. C'est la vive. Commented Apr 18, 2016 at 13:29
  • @BanksySan if you change typescript to target ES5 it will work Commented Apr 18, 2016 at 13:40
  • Cheers @DavidSherret, can I do that in WebStorm? It's be less hassle that having to use tsc at the command line. Commented Apr 18, 2016 at 14:05
  • @BanksySan you should look into using a tsconfig.json file—specify "target": "ES5" under "compilerOptions". WebStorm should support that. Commented Apr 18, 2016 at 14:15

1 Answer 1

3

As a side note regarding node.

It does support rest parameters in 5.x version. They are not enabled by default alongside with plenty other es6 goodies.

To enable them run node with --harmony parameter or if some of features are still work in progress - explicitly enable them with corresponding flags. For example:

node --harmony --harmony_default_parameters app.js

Use the following command to see all of such 'in progress' ones:

node --v8-options | grep 'in progress'
Sign up to request clarification or add additional context in comments.

3 Comments

Should node --harmony --harmony_default_parameters be node --harmony --harmony_rest_parameters?
Is there a way to enable all the bleeding edge tech?
No - for rest parameters its enough to have --harmony. As they are not in progress. So to get bleeding edge use --harmony and all 'in progress' params (its not so many) specified manually

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.