typescript 初始化包含数组的 Map

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41769955/
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:32:54  来源:igfitidea点击:

Initialize a Map containing arrays

typescript

提问by BBaysinger

I want to make a Mapwhere each member contains an array of strings. But how do I initialize and type it (in a single statement)?

我想制作一个Map每个成员都包含一个字符串数组的地方。但是我如何初始化并输入它(在单个语句中)?

I've tried this:

我试过这个:

  private _gridOptions:Map<string, Array<string>> = {"1": ["test"]};

and I get:

我得到:

Module build failed: Error: /Users/*****/Work/dashboard/src/app/core/pg.service.ts (8,5): Type '{ "1": string[]; }' is not assignable to type 'Map<string, string[]>'.

回答by GPicazo

Per Mozilla's Map documentation, you can initialize as follows:

根据Mozilla 的 Map 文档,您可以按如下方式进行初始化:

private _gridOptions:Map<string, Array<string>> = 
    new Map([
        ["1", ["test"]],
        ["2", ["test2"]]
    ]);