我有以下内容:
interface EditViewState<T> {
entity?: T;
}
abstract class EditView<T, P, S> extends React.Component<P, EditViewState<T> & S> {
constructor(props: P, ctx: any) {
super(props, ctx);
this.state = {
mode: "loading" // Type '{ entity: null; }' is not assignable to type 'Readonly<EditViewState<T> & S>'.ts(2322
}
}
componentDidMount() {
this.setState({ mode: "show" }) // Type '"show"' is not assignable to type '("loading" & S["mode"]) | ("show" & S["mode"]) | ("edit" & S["mode"])'.ts(2345)
}
}
我怎样才能使用EditViewState
并扩展它S
哪个来自具体类?
现在我有一个解决方法,使用Exclude
abstract class EditView<T, P, S> extends React.Component<P, Exclude<EditViewState<T> & S, keyof S>> {
constructor(props: P, ctx: any) {
super(props, ctx);
//@ts-ignore https://github.com/microsoft/TypeScript/issues/38947
this.state = {
mode: "loading"
}
}
componentDidMount() {
this.setState({ mode: "show" })
}
}
这样一切都很完美。具体类现在可以自动完成以下状态EditViewState
和它自己的州S
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)