pattern-matching-ts
pattern-matching-ts copied to clipboard
[feature] `matchSum`
Sometimes the name of the tag can be used as the lookup value in the struct. It's similar to fromSum
in io-ts/Encoder
.
It basically looks up the value from the sum identifier.
These types don't assume a default case currently.
declare function matchSum<S extends string>(
sum: S,
): <
A extends Record<S, string>,
M extends {
[P in A[S]]: (
value: Extract<A, Record<S, P> & Record<P, unknown>>[P],
) => unknown
},
>(
matcher: M,
) => (fa: A) => ReturnType<M[keyof M]>
declare const sumType:
| { _tag: "aside"; aside: string }
| { _tag: "bside"; bside: number }
// boolean | null
const sum = pipe(
sumType,
matchSum("_tag")({
aside: (string) => true,
bside: (number) => null,
}),
)
What are your thoughts on this, happy to accept this?
We can also derive some functions using these examples: https://serde.rs/enum-representations.html
We can also derive some functions using these examples: https://serde.rs/enum-representations.html
Hey, @waynevanson thanks a lot for the effort and the interest you showed in this! Really appreciated! 🙏
Sadly I don't have time to follow this project at the moment 😞
If you want to open a draft PR you are more than welcome but before starting I wanted you to know that I've no idea when I will be able to review it