如何利用反应选择道具类型

时间:2019-01-17 13:07:29

标签: reactjs typescript react-select

我正在尝试编写一个包装AsyncSelect的组件,但是在这种情况下,它们对props的类型是通用的,我不确定如何实现。

这是我的代码:

export class PipTagSelect extends React.Component<AsyncProps> {
    constructor(props:AsyncProps ) {
        super(props);
    }

    render() {
        return (
            <AsyncSelect
                isMulti
                cacheOptions
                {...this.props}
            />
        );
     }
}

编译器给出错误AsyncProps<OptionType>需要一个类型参数。在查看类型定义时,这很有意义。

但是,在包装组件时,我从来不需要为props提供类型参数。我不确定该怎么做。

1 个答案:

答案 0 :(得分:2)

将您的班级设为通用

 export class<T> PipTagSelect extends React.Component<AsyncProps<T>> {
    constructor(props:AsyncProps<T>) {
        super(props);
    }

    render() {
        return (
            <AsyncSelect
                isMulti
                cacheOptions
                {...this.props}
            />
        );
     }
 }

签出on github