I have a problem with React abstract generic class. When I declare one , there is missing prop on the child class and probably im doing the generic wrong. Main issue is
Type '{ moving: "no"; }' is not assignable to type 'Readonly<AbCarState & S>'
What i want to do : Make class that can be generic with default props, state and methods to extend it and create new class with possibility to change it.
import React from "react";
import { render } from "react-dom";
interface AbCarProps {
color: string;
}
interface AbCarState {
moving: string;
}
abstract class Car<P, S> extends React.Component<AbCarProps, AbCarState> {
constructor(props: P & AbCarProps) {
super(props);
this.state = {
moving: "no"
}
}
}
interface CarProps {
tires: number;
}
interface CarState {
movingFast: string;
}
class MyCar extends Car<CarProps, CarState> {
state = {
moving: "yes",
movingFast: "no"
};
render() {
return (
<div>
<div>movingFast : {this.state.movingFast}</div>
<div>tires : {this.props.tires}</div>
<div>color : {this.props.color}</div>
<div>moving : {this.state.moving}</div>
</div>
);
}
}
render(
<MyCar tires={3} color={"red"} />,
document.getElementById("root")
);
If I replace constuctor with some function then there is other or same issue:
abstract class Car<P, S> extends React.Component<AbCarProps & P, AbCarState & S> {
move = ()=>{
this.setState({moving:'yes'})
}
}
Argument of type '{ moving: "yes"; }' is not assignable to parameter of type '(AbCarState & S) | ((prevState: Readonly<AbCarState & S>, props: AbCarProps & P) => (AbCarState & S) | Pick<...>) | Pick<...>'.
Type '{ moving: "yes"; }' is not assignable to type 'Pick<AbCarState & S, "moving">'.
Types of property 'moving' are incompatible.
Type 'string' is not assignable to type 'string & S["moving"]'.
Type 'string' is not assignable to type 'S["moving"]'
So there is something im doing wrong. What could i do to make it work ? Thanks