How can I convert a Set to an Array in TypeScript

thanhpk picture thanhpk · Apr 24, 2016 · Viewed 45.1k times · Source

How can I convert a Set (eg, {2,4,6}) to an Array [2, 4, 6] in TypeScript without writing a loop explicitly ?

I have tried those following ways, all of them work in JavaScript but none of them work on TypeScript

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

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

Answer

mayan anger picture mayan anger · Jan 15, 2018

You also can do

Array.from(my_set.values());