Typescript: Set is not an array type

Created on 24 Oct 2015  ·  3Comments  ·  Source: microsoft/TypeScript

With Babel, I can use spread syntax on a Set. For example, the following neat trick returns the unique elements in an array:

const a = [ 0, 1, 2, 3, 2, 1, 0];
console.log([...new Set(a)]);

// outputs [0, 1, 2, 3]

However in Typescript, I get the following error: Set is not an array type. I presume this is a problem with the lib.d.ts typings?

BTW, I also tried this on http://www.typescriptlang.org/Playground, but there I got the error: "Cannot find the name 'Set'".
Is the Playground not up to date with the latest tsc version?

Question

Most helpful comment

I think you didn't set the target properly. Try --target ES6.

All 3 comments

I think you didn't set the target properly. Try --target ES6.

Ok, thanks @SaschaNaz

tsconfig.json
{ "compilerOptions": { "target": "es6", } }
thank you @saschanaz

Was this page helpful?
0 / 5 - 0 ratings

Related issues

disshishkov picture disshishkov  ·  224Comments

yortus picture yortus  ·  157Comments

quantuminformation picture quantuminformation  ·  273Comments

Gaelan picture Gaelan  ·  231Comments

jonathandturner picture jonathandturner  ·  147Comments