mobily / ts-belt

🔧 Fast, modern, and practical utility library for FP in TypeScript.
https://mobily.github.io/ts-belt
MIT License
1.08k stars 30 forks source link

Less strict version of match #70

Open lauritzsh opened 1 year ago

lauritzsh commented 1 year ago

In a previous project I used fp-ts which has match and a less strict matchW (short for widening).

I'm not necessarily suggesting to add this (though it would be nice to have), but how would we implement this ourselves? I especially had trouble coming up with an implementation that supports both the data-first and data-last functionality.

function matchW<A, B, R, R2>(result: Result<A, B>, okFn: (value: A) => R, errorFn: (value: B) => R2): R | R2
function matchW<A, B, R, R2>(okFn: (value: A) => R, errorFn: (value: B) => R2): (result: Result<A, B>) => R | R2

The first one was pretty trivial to write, but did not manage to have one (at least clean) implementation supporting both. Is there an easy way to implement using match? Seeing an example would help me (and maybe others) understand how to implement similar helpers.

JUSTIVE commented 5 months ago

Hi. I made a PR to resolve this issue. please have a look and let me know if it's right.