I am updating to ngrx 8, and I noticed that in the reducer
function the type of the state
parameter can be either State
or undefined
https://ngrx.io/guide/store/reducers#creating-the-reducer-function
export function reducer(state: State | undefined, action: Action) {
return scoreboardReducer(state, action);
}
Is there any reason for the undefined
optional type?
My opinion is that it is for type checking for default parameters. To set the initialState, you can do
export function reducer(state: State | undefined = {}, action: Action) {
return scoreboardReducer(state, action);
}
Check out the = {}
. If state
is undefined, it will be equal to {}
.
You can set the initialState
when undefined or just leave it.