使用静态propTypes时缺少类型

时间:2018-11-17 06:57:03

标签: reactjs typescript

我不知道发生了什么... 当我在类组件中使用静态propTypes时。事实证明该类型丢失了。

type IProps = {
    title: string;
} & ReturnType<typeof mapStateToProps> 

class Test extends PureComponent<IProps> {

    static propTypes = {

    }

    render() {
        const { title } = this.props;
        return (
            <div>hello world! {title}</div>
        )
    }
}
const mapStateToProps = (state: any) => {
    return {
        state
    }
}

export default connect(mapStateToProps)(Test);

错误消息:

[ts]
Argument of type 'typeof Test' is not assignable to parameter of type 'ComponentType<never>'.
  Type 'typeof Test' is not assignable to type 'ComponentClass<never, any>'.
    Types of property 'propTypes' are incompatible.
      Type '{}' is not assignable to type 'undefined'. [2345]

1 个答案:

答案 0 :(得分:0)

以下内容为我编译。我尚未测试验证是否可以在运行时达到您的预期。

import * as PropTypes from "prop-types";

// ...

static propTypes = {
    title: PropTypes.string.isRequired,
    state: PropTypes.any
}