【问题标题】:FlowType annotation for a parameter that must be a Class必须是类的参数的 FlowType 注释
【发布时间】:2018-06-27 21:53:32
【问题描述】:

我正在使用 Flow 在 React 项目中声明类型。在这个项目中,我有一个 intantiantes/builds 服务的功能,因此它们可以被减速器使用。提供给我的构建功能的这些服务的唯一限制是它们必须是一个类。也就是说,它们必须能够使用new 进行实例化。

我最好的解决方案(这不是解决方案)是使用 mixed 作为类型并添加运行时检查。有谁知道如何进行静态类型检查?

我使用的是 Flow 0.61.0。这是我的困境的一个基本示例。

// @flow

function buildService(
  Service: ???, 
  dependencies: {[string]: {}|Function} = {}
) {
    return new Service(dependencies)
}

【问题讨论】:

    标签: javascript class flowtype


    【解决方案1】:

    您可以使用特殊的Class<T> 实用程序类型。

    // @flow
    
    function buildService<T: {}>(
      Service: Class<T>,
      dependencies: {[string]: {}|Function} = {}
    ): T {
        return new Service(dependencies)
    }
    

    【讨论】:

      猜你喜欢
      • 2021-06-11
      • 2018-07-05
      • 2021-12-29
      • 2022-12-15
      • 2019-10-19
      • 2020-10-04
      • 2020-09-29
      • 2016-12-07
      • 2021-12-07
      相关资源
      最近更新 更多