How to iterate over a Set in TypeScript?

后端 未结 5 1887
逝去的感伤
逝去的感伤 2021-01-03 19:36

How do you iterate over a set in TypeScript? for..of does not work:

\'Set\' is not an array type or a string type

.for

5条回答
  •  时光说笑
    2021-01-03 20:23

    You can use for ... of in TypeScript if you add "es6" as "lib" in your compiler options, and "es6" as target. You may also use "es2015.iterable" in place of "es6" in your lib if that better suits your needs.

    For example (this would be your tsconfig.json):

    {
        "compilerOptions": {
            "target": "es6",
            "lib": [
                "es6",
                "dom"
            ]
        },
        "exclude": [
            "node_modules"
        ]
    }
    

    Related issue on GitHub: https://github.com/Microsoft/TypeScript/issues/12707

提交回复
热议问题