必须为Class的参数的FlowType注释

时间:2018-01-18 18:16:53

标签: javascript class flowtype

我使用Flow来声明在React项目上输入。在这个项目中,我有一个功能,它可以对服务进行内部化/构建,以便减速器可以使用它们。提供给我的构建函数的这些服务的唯一约束是它们必须是一个类。也就是说,它们必须能够使用new实例化。

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

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

// @flow

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

1 个答案:

答案 0 :(得分:1)

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

// @flow

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