简体   繁体   中英

How can I get TypeScript to build iterables in ES6 to ES5?

I am trying to create a unique array of user objects in Angular 2 writing in TypeScript. I was using the short and elegant ES6 way to create a unique Array:

  this.users = Array.from(new Set(this.users))

I get this error when I build the TypeScript to ES5:

Argument of type 'Set<{}>' is not assignable to parameter of type 'IterableShim<{}>'. Property '" es6-shim iterator "' is missing in type 'Set<{}>'.

I tried setting this.users : IterableShim<T> but IterableShim is not an acceptable type like string. The docs say String, Array, TypedArray, Map and Set are all built-in iterables, because the prototype objects of them all have a Symbol.iterator method. (Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators ) Is there a way to get TypeScript to compile ES6 that it recognizes as iterables to ES5 without errors?

Using the lib option as:

"lib": [
  "dom","es6"
]

The following works fine :

let users = []
users = Array.from(new Set(users))

More

https://basarat.gitbooks.io/typescript/content/docs/types/lib.d.ts.html#lib-option

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM