Currently implementing flow and looking to enhance the type safety of my reducers. I stumbled upon a helpful comment proposing a solution that seems compatible with my existing codebase: https://github.com/reduxjs/redux/issues/992#issuecomment-191152574
I attempted to adapt it using the %checks
keyword in flow, but encountered some issues.
Snippet from my code:
export type Action<T> = {
type: string,
payload: T,
};
interface ActionCreator<P> {
type: string;
(payload: P): Action<P>;
}
export function actionCreator<P>(type: string): ActionCreator<P> {
return Object.assign((payload: P) => ({ type, payload }), { type });
}
export function isActionOfType<P>(
action: Action<any>,
creator: ActionCreator<P>
): boolean %checks {
return action.type === creator.type;
}
When incorporating it within the reducer function like this
(...)
case isActionOfType(action, getArticles):
// action.payload still has an 'any' type
(...)
Is there a mistake on my end? Can the TypeScript solution be effectively applied in flow? Or do you recommend an alternative approach? If so, what suggestions can you offer?