Click here for a useful createReducer()
function. How would you annotate it using Flow
?
Here is an example:
// @flow
import type { TAction as TActionDefault } from '../actions/types';
type THandlers<TState, TAction> = {
[key: string]: (state: TState, action: TAction) => any
}
type TReducer<TState, TAction> = (state: TState, action: TAction) => TState
export default function createReducer<TState, TAction>(
initialState: TState,
handlers: THandlers<TState, TAction>
): TReducer<TState, TAction> {
return function reducer(state: TState = initialState, action: TAction): TState {
if (handlers[action.type]) {
return handlers[action.type](state, action)
} else {
return state
}
}
}
However, there is an issue:
Cannot access `action.type` because the property `type` is missing in `TAction` [1]. (References: [1])
And Flow does not allow specifying a default type value for TAction
.
How would you handle this situation and similar cases?