我正在尝试使用TypeScript在Angular 2中创建一个独特的用户对象数组。我使用简短而优雅的ES6方式创建了一个独特的数组:
this.users = Array.from(new Set(this.users))
当我将TypeScript构建到ES5时出现此错误:
类型'Set< {}>'的参数不能分配给'IterableShim< {}>'类型的参数。 “Set< {}>”类型中缺少属性“ es6-shim iterator ”。
我尝试设置this.users : IterableShim<T>
,但IterableShim不是像字符串那样可接受的类型。文档说String,Array,TypedArray,Map和Set都是内置的iterables,因为它们的原型对象都有一个Symbol.iterator方法。 (来源:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators)有没有办法让TypeScript编译ES6,它可以识别为ES5的可迭代而没有错误?
答案 0 :(得分:-1)
使用lib
选项:
"lib": [
"dom","es6"
]
以下工作正常:
let users = []
users = Array.from(new Set(users))
https://basarat.gitbooks.io/typescript/content/docs/types/lib.d.ts.html#lib-option