typescript { [key: string]: boolean; type { [key: string]: boolean; } 意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43396522/
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
What does the type { [key: string]: boolean; } mean?
提问by Leonid Bor
Run into such thing lately, a function declaration:
最近遇到这样的事情,一个函数声明:
static required(control: AbstractControl): {
[key: string]: boolean;
};
What is this return value? An object with arbitrary amount of properties, where each of them is a boolean and has name, which appears to be string? It's more of a typescript question I guess, but just in case someone wonders where I found that - it's Angular's Validators
class.
这个返回值是什么?具有任意数量属性的对象,其中每个属性都是布尔值并具有名称,名称似乎是字符串?我猜这更像是一个打字稿问题,但以防万一有人想知道我在哪里找到的 - 这是 Angular 的Validators
课程。
采纳答案by Leonardo Chaia
This is a key/value structure. The key is a string
and the value is a boolean
. For example:
这是一个键/值结构。键是 a string
,值是 a boolean
。例如:
let map : { [key: string]: boolean} = {};
map["foo"] = true;
map["bar"] = false;
map.foo = true;
map["foobar"] = "foo"; // Throws exception
map[1] = true; // Curiously doesn't throws exception
Check this sample on the Typescript Playground.