static propTypes和ts一起使用会导致type丢失吗?

新手上路,请多包涵

问题描述

当我使用static propTypes和typeScript和connect时,typeScript的类型校验就报错了.
告诉我类型丢了

问题出现的环境背景及自己尝试过哪些方法

我是ts新手.并没有搜索到有用的解决方法

相关代码

import React, {PureComponent} from 'react';
import { withRouter } from 'react-router';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { RootState } from '../../../reducers';

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


class Test extends PureComponent<IProps> {

    static propTypes = {
        title: PropTypes.string
    }

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

export default 
    connect(mapStateToProps)(Test)

`

你期待的结果是什么?实际看到的错误信息又是什么?

'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 '{ title: Requireable<string>; }' is not assignable to type 'undefined'. 
阅读 1.7k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题