Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert a Set to an Array in TypeScript

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' 
like image 896
thanhpk Avatar asked Apr 24 '16 21:04

thanhpk


People also ask

Can we convert array into Set?

Converting an array to Set objectutil package provides a method known as asList(). This method accepts an array as an argument and, returns a List object. Use this method to convert an array to Set.

Should I use [] or array in TypeScript?

There is no difference at all. Type[] is the shorthand syntax for an array of Type . Array<Type> is the generic syntax. They are completely equivalent.


1 Answers

You also can do

Array.from(my_set.values()); 
like image 90
mayan anger Avatar answered Sep 20 '22 05:09

mayan anger