简体   繁体   English

在 TypeScript 中将类型作为函数返回

[英]return type as function in TypeScript

I have a class named ValidatorHelper :我有一个名为ValidatorHelper的类:

class ValidatorHelper
{
   public name:string
   public value: any
  
  constructor(name:string,vlaue:any)
  {
     this.name = name
     this.value = value
  }
  toString(): string
  {
     return '"name":"' + this.name + '","value":"' + this.value + '"'
  }
 }

and a class named ValidatorBuilder和一个名为ValidatorBuilder的类

 class ValidatorBuilder {
  private validators: ValidatorHelper[] = [];
  public Build(): (name: string, value: string) => string[] {
    //how can I build the return type with this.validators
  }
  public required(): ValidatorBuilder {
    this.validators.push(new ValidatorHelper('required', true));
    return this;
  }
  public pattern(pattern: string | RegExp): ValidatorBuilder {
    this.validators.push(new ValidatorHelper('pattern', pattern));
    return this;
  }

Is there any way to build the return type of Build function with validators ?有没有办法用validators Build函数的返回类型?

Or even better ideas for my ValidatorBuilder或者对我的ValidatorBuilder更好的想法

I think you miss the point of validation.我认为你错过了验证的重点。 Validation checks one element and returns a boolean for the result.验证检查一个元素并为结果返回一个布尔值。 You can specify this by an interface.您可以通过接口指定这一点。

interface Validator {
  validate: (value: any) => boolean;
}

Now you have different kind of validations.现在您有不同类型的验证。 In your example Required and Pattern.在您的示例中为必需和模式。 Let's define those.让我们定义这些。

class RequiredValidator implements Validator {
  validate(value: any) {
    return !!value;
  }
}

class RequiredValidator implements Validator {

  regularExpression: RegExp;

  constructor(regularExpression: string | RegExp) {
    this.regularExpression = typeof regularExpression === 'string' ? new RegExp(regularExpression) : regularExpression;
  }

  validate(value: string): boolean {
    return this.regularExpression.test(value);
  }
}

Now you can add your builder.现在您可以添加您的构建器。 The build step also needs to return a validator or a validate function.构建步骤还需要返回验证器或验证函数。 After all this is nothing else but a chaining of all stored validations.毕竟这只是所有存储验证的链接。

class ValidatorBuilder {
  private validators: Validator[] = [];

  public required(): ValidatorBuilder {
    this.validators.push(new RequiredValidator());
    return this;
  }

  public pattern(pattern: string | RegExp): ValidatorBuilder {
    this.validators.push(new PatternValidator(pattern));
    return this;
  }

  public build(): Validator {
    return {
      validate: (value: any) => this.validators.every(validator => validator.validate(value)),
    };   
  }
}

Test it:测试一下:

const myValidator: Validator = new ValidatorBuilder().required().pattern('[\\d]+').build();

console.log('required fails', myValidator.validate(null));
console.log('pattern fails', myValidator.validate('abc'));
console.log('all ok',  myValidator.validate('123'));

See: https://stackblitz.com/edit/typescript-sb5yny请参阅: https : //stackblitz.com/edit/typescript-sb5yny

Please note, that this example still have some bugs.请注意,这个例子仍然有一些错误。 You can easily run into misbehavior, when you pass for example an object.例如,当您传递一个对象时,您很容易遇到不当行为。 This simple quick and dirty example is only for demonstration.这个简单的快速而肮脏的示例仅用于演示。

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

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