Does TypeScript have the equivalent of ES6 "Sets"

I want to extract all unique properties from an array of objects, you can do this in ES6 very simply using the spread operator and Set so:

var arr = [ {foo:1, bar:2}, {foo:2, bar:3}, {foo:3, bar:3} ] const uniqueBars = [... new Set(arr.map(obj => obj.bar))]; >> [2, 3] 

However, in TypeScript 1.8.31 this gives me a build error:

Unable to find Install Name

I know that I can make VS ignore it using

 declare var Set; 

But I hope something TypeScript will compile in non-ES6 so that it can be used on older systems.

Does anyone know if there is such an opportunity that I could use?

Edit

In fact, even when I use declare var Set; , the above code compiles, but repeatedly repeats this error, so I'm not sure how to use it even without compilation:

Uncaught TypeError: (intermediate value) .slice is not a function

How can I update my code to use Set in TypeScript?

+5
source share
3 answers

No. If you compile ES5 or later, Typescript adds syntax changes from ES6. It does not add any of the standard library objects.

If you need the ones that I suggest you look at something like core.js

+2
source

It worked for me.

One problem is that typescript is trying to use

 ERROR TypeError: (intermediate value).slice is not a function 

instead of Array.from ();

anyway this code worked for me in my Angular 4 application

 Array.from(new Set(Array)).sort(this.compareNumbers) 

hope this helps someone

+1
source

You can use this type of script library . Or maybe create your one set class using the link from this library

0
source

Source: https://habr.com/ru/post/1263145/


All Articles