Adding Types to the Reducer
Learn how to add types to the reducer and make constants of type names.
We'll cover the following...
Implementing types in the reducer
Adding TypeScript will protect us from some of the more obvious mistakes—by ensuring we both pass in the correct arguments and return the expected result. Both situations can be tricky to triage in our applications because they might not happen until after the user takes an action—like clicking a button in the UI. Let’s quickly add some types to our reducer:
Let’s explain the code sample above:
Lines 1–2: We define two type definitions: the
CounterState
ofnumber
type to represent the state’s shape and theCounterAction
ofstring
type to represent the action type. ...