2017-04-13 4 views
4

最近、そのようなことに遭遇し、関数の宣言:タイプ{[キー:文字列]:ブール値。 }意味ですか?

static required(control: AbstractControl): { 
     [key: string]: boolean; 
    }; 

この戻り値は何ですか?任意の量のプロパティを持つオブジェクト。各オブジェクトはブール値で、名前は文字列のように見えますか?それは私が推測するタイプスクリプトの質問の多くですが、誰かが私がそれを見つけたのか不思議に思うような場合に備えて - AngularのValidatorsクラスです。

+2

これはドキュメントの説明です。 "索引付け可能な型"を探します。 –

+1

ここに:https://www.typescriptlang.org/docs/handbook/interfaces.html#indexable-types – Paleo

答えて

8

これはキー/値の構造です。キーはstringで、値はbooleanです。たとえば、次のように

let map : { [key: string]: boolean} = {}; 
map["foo"] = true; 
map["bar"] = false; 
map["foobar"] = "foo"; // Throws exception 
map[1] = true; // Curiously doesn't throws exception 
map.foo = true; // Throws exception 

チェックthis sample on the Typescript Playground.

Indexable Types

+0

もし私が 'console.log(map.foo)'を実行すればOKでしょうか? –

+1

ちょっと@LeonidBor、私は答えを改善し、サンプルを追加しました。それが役に立てば幸い。 –

関連する問題