最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Does TypeScript have an equivalent of ES6 "Sets" - Stack Overflow

programmeradmin3浏览0评论

I want to extract all the unique properties from an array of objects, you can do so in ES6 very cleanly using the spread operator and the 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 the build error:

Cannot find name 'Set'

I know I can force VS to ignore it by using

declare var Set;

But I'm hoping for something TypeScript will compile into non-ES6 so that it could be used on older systems.

Does anyone know if there's such a feature I could use?

Edit:

Actually, even when I use declare var Set;, the above code compiles but throws this error repeatedly, so I'm not sure how to use it even without compiling down:

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

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

I want to extract all the unique properties from an array of objects, you can do so in ES6 very cleanly using the spread operator and the 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 the build error:

Cannot find name 'Set'

I know I can force VS to ignore it by using

declare var Set;

But I'm hoping for something TypeScript will compile into non-ES6 so that it could be used on older systems.

Does anyone know if there's such a feature I could use?

Edit:

Actually, even when I use declare var Set;, the above code compiles but throws this error repeatedly, so I'm not sure how to use it even without compiling down:

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

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

Share Improve this question edited Jan 21, 2017 at 19:42 Charles Clayton asked Jan 21, 2017 at 19:22 Charles ClaytonCharles Clayton 17.9k13 gold badges93 silver badges122 bronze badges 3
  • 2 Possible duplicate of Angular 2 typescript can't find names – jonrsharpe Commented Jan 21, 2017 at 19:28
  • TS doesn't have to have an equivalent of this, because TS transpiles to JS, and Set is polyfillable JS feature. This is a duplicate, stackoverflow.com/a/41608156/3731501 in particular. – Estus Flask Commented Jan 21, 2017 at 20:01
  • 1 I suppose you could also compile the typescript to ES6 code and then transpile that to ES5 with something like babel. That would allow you to use all the ES6 collections. – jfriend00 Commented Jan 21, 2017 at 20:29
Add a comment  | 

3 Answers 3

Reset to default 8

This worked for me.

One of the issues appears to be that typescript trys to use

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

instead of Array.from();

in any event this code worked for me in my Angular 4 applicaiton

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

hope this helps someone

No. If you compile to ES5 or older Typescript only adds the syntactic changes from ES6. It doesn't add any of the standard library objects.

If you want those I suggest you look into something like core.js

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

发布评论

评论列表(0)

  1. 暂无评论