Class Components
Within TypeScript, React.Component
is a generic type (aka React.Component<PropType, StateType>
), so you want to provide it with (optional) prop and state type parameters:
View in the TypeScript Playground
Don't forget that you can export/import/extend these types/interfaces for reuse.
Why annotate state
twice?
It isn't strictly necessary to annotate the state
class property, but it allows better type inference when accessing this.state
and also initializing the state.
This is because they work in two different ways, the 2nd generic type parameter will allow this.setState()
to work correctly, because that method comes from the base class, but initializing state
inside the component overrides the base implementation so you have to make sure that you tell the compiler that you're not actually doing anything different.
No need for readonly
You often see sample code include readonly
to mark props and state immutable:
This is not necessary as React.Component<P,S>
already marks them as immutable. (See PR and discussion!)
Class Methods: Do it like normal, but just remember any arguments for your functions also need to be typed:
View in the TypeScript Playground
Class Properties: If you need to declare class properties for later use, just declare it like state
, but without assignment: