Javascript 如何使用defaultProps声明react redux组件流中的类型?

Javascript 如何使用defaultProps声明react redux组件流中的类型?,javascript,react-redux,flowtype,flow-typed,Javascript,React Redux,Flowtype,Flow Typed,下面带有类型干扰的示例1起作用,但我试图声明导出以改进类型检查的尝试失败(请参见示例2和示例3) /@flow 从“React”导入*作为React 从“react redux”导入{connect} //如果Props是一个精确的类型{|…|},下面的流错误将是相同的 类型道具={ 建议1:数字, 建议2:数字, } 类ExampleClass扩展了React.Component{ 静态defaultProps={prop2:1} render(){ 返回this.props.prop1+th

下面带有类型干扰的示例1起作用,但我试图声明导出以改进类型检查的尝试失败(请参见示例2和示例3)

/@flow
从“React”导入*作为React
从“react redux”导入{connect}
//如果Props是一个精确的类型{|…|},下面的流错误将是相同的
类型道具={
建议1:数字,
建议2:数字,
}
类ExampleClass扩展了React.Component{
静态defaultProps={prop2:1}
render(){
返回this.props.prop1+this.props.prop2
}
}
//有效,但示例1的类型是什么?
导出常量Example1=connect(null,null)(ExampleClass)
//无法将connect(…)(…)分配给Example2,因为未定义的[1]与类型参数P[2]的属性prop2中的数字[1]不兼容。
导出常量Example2:React.ComponentType=connect(null,null)(ExampleClass)
导出常量示例2=
//无法将connect(…)(…)分配给示例3,因为
//属性prop1在Props[1]中是只读的,但在类型参数P[3]的对象类型[2]中是可写的。
//属性prop2在Props[1]中是只读的,但在类型参数P[3]的对象类型[2]中是可写的。
导出常量Example3:React.ComponentType=connect(null,null)(ExampleClass)
导出常量示例3=
//请注意,这是可行的,但我正在寻找一种方法来声明
//不重复属性列表的类型
导出常量Example2a:React.ComponentType=connect(null,null)(ExampleClass)

参考这里的代码

这应该是一个注释,但我还不能。无论如何,看起来您的示例repo使用的是旧版本的Flow。从0.89.0版开始,(HOCs)有了巨大的增长。如果您仍然面临此问题或类似问题,我建议您升级到0.89.0,使用,然后再次调查

// @flow

import * as React from 'react'
import { connect } from 'react-redux'

// flow errors below will be the same if Props is an exact type {|...|}
type Props = {
    prop1: number,
    prop2: number,
}

class ExampleClass extends React.Component<Props> {
    static defaultProps = { prop2: 1 }

    render() {
        return this.props.prop1 + this.props.prop2
    }
}

// works, but what is the type of Example1?
export const Example1 = connect(null, null)(ExampleClass)

// Cannot assign connect(...)(...) to Example2 because undefined [1] is incompatible with number [1] in property prop2 of type argument P [2].
export const Example2: React.ComponentType<Props> = connect(null, null)(ExampleClass)
export const example2 = <Example2 prop1={1} />

// Cannot assign connect(...)(...) to Example3 because
// property prop1 is read-only in Props [1] but writable in object type [2] in type argument P [3].
// property prop2 is read-only in Props [1] but writable in object type [2] in type argument P [3].
export const Example3: React.ComponentType<React.ElementConfig<React.ComponentType<Props>>> = connect(null, null)(ExampleClass)
export const example3 = <Example3 prop1={1} />

// Note that this works but I'm looking for a way to declare 
// the type without repeating the list of properties
export const Example2a: React.ComponentType<{prop1: number, prop2?: number}> = connect(null, null)(ExampleClass)