如何在 TypeScript 中将 Set 转换为数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36829184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:05:38 来源:igfitidea点击:
How can I convert a Set to an Array in TypeScript
提问by thanhpk
How can I convert a Set (eg, {2,4,6}) to an Array [2, 4, 6] in TypeScript without writing a loop explicitly ?
如何在 TypeScript 中将 Set(例如 {2,4,6})转换为数组 [2, 4, 6] 而不显式编写循环?
I have tried those following ways, all of them work in JavaScript but none of them work on TypeScript
我尝试了以下方法,它们都在 JavaScript 中工作,但没有一个在 TypeScript 上工作
[...set] // ERR: "Type 'Set<{}>' is not an array type" in typescript
Array.from(set) // ERR: Property 'from' does not exist on type 'ArrayConstructor'
采纳答案by basarat
Fix
使固定
- Use tsconfig.json with
"lib": ["es6"]
- 使用 tsconfig.json 与
"lib": ["es6"]
More
更多的
- Google
lib
option. - I wrote some docs too : https://basarat.gitbooks.io/typescript/content/docs/types/lib.d.ts.html#lib-option
回答by mayan anger
You also can do
你也可以做
Array.from(my_set.values());