Use TypeScript to Type React useReducer

Share this video with your friends

Send Tweet

We get the state of our board from a reduce function. It currently has no error but also has no type safety since everything is inferred to be of type any. useReducer is generic and accepts type arguments, describing the state as well as the events it can expect to receive.

The simplest way to add types to a useReducer is to add them directly to the reducer function itself. We will do this by using an alias for our types.

~ a year ago

Could you, please, explain, how you get types in row: let [{ gameState, cells, mines }, send] = React.useReducer

I've seen this video a couple times and didn’t get types in a variable 😪 😪