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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:41:58  来源:igfitidea点击:

What does the type { [key: string]: boolean; } mean?

typescript

提问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 Validatorsclass.

这个返回值是什么?具有任意数量属性的对象,其中每个属性都是布尔值并具有名称,名称似乎是字符串?我猜这更像是一个打字稿问题,但以防万一有人想知道我在哪里找到的 - 这是 Angular 的Validators课程。

采纳答案by Leonardo Chaia

This is a key/value structure. The key is a stringand 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.

在 Typescript Playground 上查看此示例。

Indexable Types.

可索引类型