Skip to content

Instantly share code, notes, and snippets.

@anthonyjoeseph
Created June 1, 2020 03:32
Show Gist options
  • Save anthonyjoeseph/8489a62ee372dd088a3e5c981406fc10 to your computer and use it in GitHub Desktop.
Save anthonyjoeseph/8489a62ee372dd088a3e5c981406fc10 to your computer and use it in GitHub Desktop.
Redux Reducer + morphic-ts
import { Reducer, Action } from "redux";
import { ADT } from '@morphic-ts/adt';
export const safeReducer = <A extends Action, S, tag extends keyof A & string>(
curriedReducer: (a: A) => (s: S) => S,
defaultState: S,
adt: ADT<A, tag>
): Reducer<S, A> => (
nullableState,
action
) => {
if (!nullableState || !adt.verified(action)) return defaultState;
return curriedReducer(action)(nullableState);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment