fp-ts
fp-ts copied to clipboard
What is the idiomatic way to do pattern matching in the fp-ts ecosystem
Hi I'm pretty new to fp concepts but I'm starting to work on some demos with the library
I want to do something like
interface ICounterActions {type: "increment" | "decrement"}
const counterReducer = (state: number, action: ICounterActions) => {
return pipe(
{ state, ...action },
match<ICounterActions>("type")({
increment: state => state + 1,
decrement: state => state - 1,
a: () => {}, // would like compile error
})
);
};
is there a way of doing this with fo-ts alone? I browsed through the modules several times but I could be missing it because I don't know a term.
is there a recommended library?
I realize I could just use switch
but it's not pipeable.
Thanks in advance.
There's no module in fp-ts
for generic pattern matching, all data types have its own specialized match
function, you can use something like ts-pattern
Would it make sense to have Record.match to do this?
Would also like the options to match("key")([predicate, operation])(value)
Perhaps https://github.com/pfgray/ts-adt is more close to what you're looking for.
all data types have its own specialized match function, you can use something like ts-pattern
Can you give an example? I'm finding very hard to use fp-ts from TS (mind you, I don't have zero FP experience, I come from F#).
There's also @unsplash/sum-types if you can accept the constraint of no generics.
I use ts-adt like @DenisFrezzato mentioned
use it like:
import { makeMatch } from "ts-adt/MakeADT";
interface ICounterActions {type: "increment" | "decrement"}
matchType = makeMatch("type");
const counterReducer = (state: number, action: ICounterActions) => {
return pipe(
action,
matchType({
increment: ()=> { state => state + 1 },
decrement: ()=> { state => state - 1 },
})
};
This will throw an error if have not implemented a type or implement a type that does not exist. I personally like this because if I make a change it forces me to handle the new type anywhere I match it.
This video explains it well
Holy shit that's ugly