Created
June 1, 2020 03:32
-
-
Save anthonyjoeseph/8489a62ee372dd088a3e5c981406fc10 to your computer and use it in GitHub Desktop.
Redux Reducer + morphic-ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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