[英]What does “(control: AbstractControl): {[key: string]: any} | null =>” do?
I'm new to TypeScript and Angular but I have a pretty good background in C#. 我是TypeScript和Angular的新手,但是我在C#中有相当不错的背景。
I'm reading this article , specifically "Custom validators" and this snippet 我正在阅读本文 ,特别是“自定义验证器”和此代码段
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;
};
}
What does this line of code do/mean? 这行代码做什么/意味着什么? Is this TypeScript's way of defining a delegate? 这是TypeScript定义委托的方式吗?
(control: AbstractControl): {[key: string]: any} | null => {
This is an arrow function
. 这是arrow function
。 To understand it, you can read it as 要了解它,您可以将其阅读为
function (control: AbstractControl): {[key: string]: any} | null {
const forbidden = nameRe.test(control.value);
return forbidden ? {'forbiddenName': {value: control.value}} : null;
};
That is, the function returns type {[key: string]: any}
(an object with key(s) declared as variable named key
, and value type can be any), or null
; 也就是说,该函数返回类型{[key: string]: any}
(具有声明为key
变量的对象,其名称为key
,值类型可以为any),或者返回null
; while the function requires input value with Angular type AbstractControl
. 而该函数需要Angular类型AbstractControl
输入值。
In TypeScript, a colon is frequently used in two ways: 在TypeScript中,冒号通常以两种方式使用:
{ key: value }
在一个对象中,例如{ key: value }
Hope this helps. 希望这可以帮助。 Welcome to the world of Angular & TypeScript! 欢迎来到Angular和TypeScript的世界!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.