我正在尝试为我的任何与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>
有关。如何设置类的默认状态属性列表?