Map<String,String> Dart 在 Typescript 中的等价物?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52562636/
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
Map<String,String> Dart equivalent in Typescript?
提问by Luke Pighetti
I am trying to find an equivalent to Map<String, String>
in Dart, but usable in Typescript. This is a quick way to define a string:string key:value store.
我试图Map<String, String>
在 Dart 中找到等价物,但可以在 Typescript 中使用。这是定义 string:string key:value 存储的快速方法。
Also, this can be used like this, which is my next question:
此外,这可以像这样使用,这是我的下一个问题:
Map<String, List<String>>
Map<String, List<String>>
This would define a key:value store where the keys are strings and the values are a list of strings.
这将定义一个 key:value 存储,其中键是字符串,值是字符串列表。
Is there any way to do either in Typescript?
有什么办法可以在打字稿中做到吗?
回答by Daniel Earwicker
The Typescript equivalent would be:
打字稿等价物将是:
const m = new Map<string, string[]>();
m.set("fruit", ["apple", "banana"]);
Note that string[]
is the type "array of strings", which despite the name is a dynamically growable list of strings (unlike the fixed length arrays in many popular statically typed languages).
请注意,这string[]
是“字符串数组”类型,尽管名称是一个动态可增长的字符串列表(与许多流行的静态类型语言中的固定长度数组不同)。
You can say Array<string>
instead but this is probably less widely used.
你可以说,Array<string>
但这可能不太广泛使用。
If your key is a string, you may find it more convenient to use a JS object. Objects in JS are basically maps with string keys. In TS the example becomes:
如果您的键是字符串,您可能会发现使用 JS 对象更方便。JS 中的对象基本上是带有字符串键的映射。在 TS 中,示例变为:
type StringToArrayOfStrings = {
[name: string]: string[]
};
const m: StringToArrayOfStrings = {};
m["fruit"] = ["apple", "banana"];
In the type
declaration we state the signature of the indexing operator [...]
. (It's possible to have numeric keys as well as string ones.)
在type
声明中,我们声明了索引运算符的签名[...]
。(可以有数字键和字符串键。)
UPDATE
更新
Still using a plain object as a map, note that we don't have to use a type
declaration to give a name to a type. The type declaration can be substituted in-place, and we can then immediately initialise m
with a compatible object:
仍然使用普通对象作为映射,注意我们不必使用type
声明来为类型命名。类型声明可以就地替换,然后我们可以立即m
使用兼容对象进行初始化:
const m: { [name: string]: string[] } = {
"fruit": ["apple", "banana"],
"spaceship": ["orion", "apollo"]
};
Note that if we omit the type annotation on m
:
请注意,如果我们省略 上的类型注释m
:
const m = {
"fruit": ["apple", "banana"],
"spaceship": ["orion", "apollo"]
};
then the type of m
is inferred to be more restrictive:
那么m
推断出的类型更具限制性:
{
fruit: string[];
spaceship: string[];
}
the compiler will assume we mean that m
is permanently stuck with just two properties, fruit
and spaceship
, and will not allow us to add more. Interestingly, it will not assume anything about the length of the arrays (even it is possible to describe such things in TS via tuple types).
编译器会假设我们的意思m
是永久停留在只有两个属性,fruit
and spaceship
,并且不允许我们添加更多属性。有趣的是,它不会对数组的长度做任何假设(即使可以通过元组类型在 TS 中描述这些东西)。
回答by Ebuall
If keys are just strings, you probably would prefer to use plain objects in TypeScript, which you can type as Record<string, string>
.
Example usage
如果键只是字符串,您可能更喜欢在 TypeScript 中使用普通对象,您可以将其键入为Record<string, string>
.
示例用法
回答by Mike Lischke
Typescript is a superset of Javascript, which has a built-in Map class. This can take any key/value pair, including String, List<String>
. Well, there's no List
type in JS, but you can use Array
instead.
Typescript 是 Javascript 的超集,它有一个内置的 Map 类。这可以采用任何键/值对,包括String, List<String>
. 好吧,List
JS 中没有类型,但您可以使用它Array
。