I want to use component inheritance, allowing common states in the abstract class and other ones in the inheritor. How do I combine this two types of states correctly? I am trying to Omit an external generic, but I still get a type error when i try to set states.
My code example:
import * as React from "react";
type CoreState = {
coreState1: number;
coreState2: string;
};
abstract class CoreComponent<OuterState = {}> extends React.Component<{}, Omit<OuterState, keyof CoreState> & CoreState> {
componentDidMount() {
this.setState({
coreState1: 1,
coreState2: "test",
});
}
}
type BaseState = {
state1: number;
state2: string;
};
class AnotherComponent<OuterState = {}> extends CoreComponent<Omit<OuterState, keyof BaseState> & BaseState> {
componentDidMount() {
super.componentDidMount();
this.setState({
state1: 1,
state2: "test",
});
}
render() {
return null;
}
}
Error i got:
TS2345: Argument of type '{ state1: 1; state2: "test"; }' is not assignable to parameter of type '(Pick<Pick<OuterState, Exclude<keyof OuterState, "state1" | "state2">> & BaseState, "state1" | "state2" | Exclude<Exclude<keyof OuterState, "state1" | "state2">, "coreState1" | "coreState2">> & CoreState) | ((prevState: Readonly<...>, props: Readonly<...>) => (Pick<...> & CoreState) | ... 1 more ... | null) | Pick<....'.
Types of property 'state1' are incompatible.
Type '1' is not assignable to type '(Pick<OuterState, Exclude<keyof OuterState, "state1" | "state2">> & BaseState)["state1"] | (Pick<Pick<OuterState, Exclude<...>> & BaseState, "state1" | ... 1 more ... | Exclude<...>> & CoreState)["state1"] | undefined'.
Error screenshot: https://yadi.sk/i/ByOr05JIauRyfQ
Omit<OuterState, keyof CoreState> & CoreState
?