Run into such thing lately, a function declaration:
static required(control: AbstractControl): { [key: string]: boolean; };
What is this return value? An object with an arbitrary amount of properties, where each of them is a boolean and has a name, which appears to be a 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.
The {[key: string]: string} syntax is an index signature in TypeScript and is used when we don't know all the names of a type's properties ahead of time, but know the shape of the values. The index signature in the examples means that when an the object is indexed with a string , it will return a string .
KeyString ( _KeyCode_ , _KeyCode2_ ) expression A variable that represents an Application object.
keyof is a keyword in TypeScript which is used to extract the key type from an object type.
This is a key/value structure, named index signatures (or previously known as indexable Types) in typescript.
The key is a string
and the value is a boolean
. For example:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With