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'
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.
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.
You also can do
Array.from(my_set.values());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With