打字稿“ React基类默认状态属性的类型不能分配给Pick <State“>

时间:2020-04-27 18:26:36

标签: reactjs typescript

我正在尝试为我的任何与RX绑定的组件创建基类以进行扩展

abstract class RxJSReactHelperClass<P, S> extends React.Component<P, S & Errors> {
    protected disposer: Subscription = new Subscription()
    protected subjectDisposer: BehaviorSubject<any>[] = []

    protected handleError = (error: Error | unknown) => {
        let errorText = error instanceof Error ? error.message : JSON.stringify(error)
        console.error(error)
        this.setState({ error: [...this.state.error, errorText] })
    }

    public abstract componentWillUnmount(): void
}

,因此实现componentWillUnmount需要任何组件。该部分有效,但在setState行上出现错误:

Argument of type '{ error: string[]; }' is not assignable to parameter of type '(S & Errors) | ((prevState: Readonly<S & Errors>, props: Readonly<P>) => (S & Errors) | Pick<S & Errors, "error"> | null) | Pick<...> | null'.


Type '{ error: string[]; }' is not assignable to type 'Pick<S & Errors, "error">'.
    Types of property 'error' are incompatible.
      Type 'string[]' is not assignable to type 'S["error"] & string[]'.
        Type 'string[]' is not assignable to type 'S["error"]'.
我知道的

React.Component<P, S & Errors>有关。如何设置类的默认状态属性列表?

0 个答案:

没有答案
相关问题