简体   繁体   English

“(control:AbstractControl):{[key:string]:any} |是什么? null =>”吗?

[英]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中,冒号通常以两种方式使用:

  1. In an object, such as { key: value } 在一个对象中,例如{ key: value }
  2. Define the type of a value, which might be your question in this case. 定义值的类型,在这种情况下可能是您的问题。

Hope this helps. 希望这可以帮助。 Welcome to the world of Angular & TypeScript! 欢迎来到Angular和TypeScript的世界!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 属性 ... 不存在于类型 &#39;{ [key: string]: AbstractControl; }&#39; - Property … does not exist on type ‘{ [key: string]: AbstractControl; }’ 属性&#39;xxxx&#39;不存在于类型&#39;{{键:字符串]:AbstractControl; }” - Property 'xxxx' does not exist on type '{ [key: string]: AbstractControl; }' Validators.nullValidator(control:AbstractControl)有什么意义? - What is the point of Validators.nullValidator(control: AbstractControl)? (控件:AbstractControl): - (control: AbstractControl) : 键入“抽象控件 | null' 不可分配给类型 'NgIterable<any> | null | 不明确的'</any> - Type 'AbstractControl | null' is not assignable to type 'NgIterable<any> | null | undefined' 在“{ [key: string]: AbstractControl; 类型上找不到带有“string”类型参数的索引签名 } - No index signature with a parameter of type 'string' was found on type '{ [key: string]: AbstractControl; } TypeScript:{[key: string]: any} 作为函数的返回类型是什么意思? - TypeScript: What does {[key: string]: any} means as return type of a function? Angular - 对象可能为“null”:错误 TS7052:元素隐式具有“any”类型,因为类型“AbstractControl” - Angular - Object is possibly 'null': error TS7052: Element implicitly has an 'any' type because type 'AbstractControl' 角度错误:类型&#39;AbstractControl | null&#39; 不可分配给类型 &#39;AbstractControl&#39; - Angular Error: Type 'AbstractControl | null' is not assignable to type 'AbstractControl' 为什么我收到一条错误消息,指出错误 TS2339:类型“AbstractControl”上不存在属性“控件” - Why do i get an error saying error TS2339: Property 'controls' does not exist on type 'AbstractControl'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM