I'm currently learning TypeScript and Angular. While reading about Custom validators I came across the following piece of code from https://angular.io/guide/form-validation .
export function forbiddenNameValidator(nameRe: RegExp): ValidatorFn {
return (control: AbstractControl): {[key: string]: any} | null => {
const forbidden = nameRe.test(control.value);
return forbidden ? {'forbiddenName': {value: control.value}} : null;
};
}
I don't really understand what return type of the inner function ie {[key: string]: any}
means? I understand key:string
part, ie Object's key is of type string but what exactly {[key: string]: any}
means?
It means the function returns an object that you can index into with any string value; the property's result value type is any
meaning it can be, well, anything. (The | null
means it can also return null
instead of returning an actual object.)
Object −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−v−−−−−−−−−−−−−−−−−−v {[key: string]: any} Key of all properties is any string −−−−−^^^^^^^^^^^^^ ^^^−−−−− type of all properties is `any`
It's a very broad type.
More in the documentation of index signatures .
It returns an object, like
{
"name": "John"
}
or
{
"length": 5
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.