Class Components
React.Component
is used to describe a class component. React.Component
is a generic type that takes two optional type variables, PropType
and StateType
.
The following example is from the react-typescript-cheatsheet.
type MyProps = {// using `interface` is also okmessage: string;};type MyState = {count: number; // like this};class App extends React.Component<MyProps, MyState> {state: MyState = {// optional second annotation for better type inferencecount: 0,};render() {return (<div>{this.props.message} {this.state.count}</div>);}}
This video explains another example of typing a class component.