Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] matchSum #8

Open
waynevanson opened this issue Oct 3, 2021 · 2 comments
Open

[feature] matchSum #8

waynevanson opened this issue Oct 3, 2021 · 2 comments
Assignees
Labels
enhancement New feature or request

Comments

@waynevanson
Copy link

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?

@stefano-regosa-deel stefano-regosa-deel self-assigned this Oct 10, 2021
@waynevanson
Copy link
Author

We can also derive some functions using these examples:
https://serde.rs/enum-representations.html

@stefano-regosa-deel
Copy link
Owner

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

@stefano-regosa-deel stefano-regosa-deel added the enhancement New feature or request label Apr 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants