【问题标题】:Return type of anonymous function [duplicate]匿名函数的返回类型[重复]
【发布时间】:2019-05-04 23:11:45
【问题描述】:

我有一个这样的匿名函数:

static oneOf(options: any[], cb?: Function) ValidatorFn {
  ....
}

我想指定 cb 必须返回一个布尔值,但我是一个打字稿新手,不知道我是否可以(或者即使我这样做会对解析器产生影响)。

我该怎么做?

【问题讨论】:

    标签: typescript


    【解决方案1】:

    您可以使用函数签名来指定回调的实际签名:

    static oneOf(options: any[], cb?: () => boolean ): ValidatorFn { // cb takes no parameters returns a boolean
      ....
    }
    

    您还可以指定回调参数:

    static oneOf(options: any[], cb?: (a: string) => boolean ): ValidatorFn { // cb takes a single string parameter returns a boolean
      ....
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-10
      • 2012-04-30
      相关资源
      最近更新 更多