简体   繁体   English

TypeScript 接口 function 语法

[英]TypeScript interface function syntax

I want to understand the syntax of the following TypeScript function.我想了解以下 TypeScript function 的语法。

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;
                          };
}

Here: AbstractControl: https://angular.io/api/forms/AbstractControl这里:AbstractControl: https://angular.io/api/forms/AbstractControl

My understanding is:我的理解是:

  1. forbiddenNameValidator : name of the user defined function.禁止名称验证器:用户定义的名称 function。

  2. nameRe: RegExp : Argument to function - forbiddenNameValidator . nameRe: RegExp : function- forbiddenNameValidator的参数。

  3. ValidatorFn : Return type of the function - forbiddenNameValidator . ValidatorFn : function- forbiddenNameValidator的返回类型。

From here: https://angular.io/api/forms/ValidatorFn从这里开始: https://angular.io/api/forms/ValidatorFn

interface ValidatorFn
{
  (control: AbstractControl): ValidationErrors | null
}

That documentation says that (control: AbstractControl): ValidationErrors | null该文档说(control: AbstractControl): ValidationErrors | null (control: AbstractControl): ValidationErrors | null is a function. (control: AbstractControl): ValidationErrors | null是 function。

Which symbol here shows that it is a function?这里的哪个符号表明它是 function? I had read that => symbol represents functions.我读过=>符号代表函数。

Which symbol here shows that it is a function?这里的哪个符号表明它是 function? I had read that => symbol represents functions.我读过 => 符号代表函数。

There's a few different ways to create function types.有几种不同的方法可以创建 function 类型。 The one you see here only occurs if you're defining an interface, and want to specify that that interface is also callable.您在此处看到的只有在您定义一个接口并且想要指定该接口也是可调用的时才会出现。 The syntax is to have the argument list on the left hand side wrapped in parentheses, then a colon, then the return value on the right hand side.语法是将左侧的参数列表用括号括起来,然后是冒号,然后是右侧的返回值。

You can see more about this particular syntax here: https://www.typescriptlang.org/docs/handbook/interfaces.html#function-types您可以在此处查看有关此特定语法的更多信息: https://www.typescriptlang.org/docs/handbook/interfaces.html#function-types

And other ways of doing functions here, including the => version you're familiar with: https://www.typescriptlang.org/docs/handbook/functions.html以及这里做函数的其他方式,包括你熟悉的=>版本: https://www.typescriptlang.org/docs/handbook/functions.html

How does control: AbstractControl receive the argument in the following part of that code? control: AbstractControl如何在该代码的以下部分接收参数?

These lines are merely creating and then returning the validator function.这些行只是创建然后返回验证器 function。 No control object exists yet.尚无control object 存在。 Eventually someone will call this new validator function, and it will be their responsibility to pass in an appropriate object.最终有人会调用这个新的验证器 function,他们有责任传入一个合适的 object。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM