2016-04-24 434 views
6

如何在TypeScript中將Set(例如,{2,4,6})轉換爲Array [2,4,6]而不顯式編寫循環?如何將Set轉換爲TypeScript中的數組

我已經試過那些以下方式,所有的人都在JavaScript中工作,但他們沒有對打字稿工作

[...set] // ERR: "Type 'Set<{}>' is not an array type" in typescript 

Array.from(set) // ERR: Property 'from' does not exist on type 'ArrayConstructor' 
+1

你編譯到什麼樣的目標? Set和Array.from都是在ES6中添加的,並且在編譯到ES5時不可用。如果你想使用它們,你可以嘗試使用core.js來爲它們獲取polyfils。 – toskv

+0

@toskv你說得對,當我將目標選項更改爲'ES6'時,它工作正常,當前目標是'ES5' – thanhpk

回答

4

你也可以做

Array.from(MySet.values());