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

Match multiple patterns simultaneously #11

Open
TmLev opened this issue May 25, 2022 · 6 comments
Open

Match multiple patterns simultaneously #11

TmLev opened this issue May 25, 2022 · 6 comments

Comments

@TmLev
Copy link

TmLev commented May 25, 2022

Hi!

Thanks for this useful package.

I was wondering whether it is possible to match multiple patterns simultaneously? Like so:

const LoadState = makeTaggedUnion({
  Unstarted: none,
  Loading: (percentLoaded: number) => percentLoaded,
  Loaded: (response: Buffer) => response,
  Error: (error: Error) => error,
});

type LoadState = MemberType<typeof LoadState>;

const state: LoadState = LoadState.Unstarted;

console.log("State is ", state.match({
  Unstarted | Error: () => "unavailable", // `Unstarted` AND `Error` simultaneously.
  _: () => "available", 
}));

I'm assuming it's not possible since it requires changes to how TypeScript treats Something | AnotherThing syntax, but maybe I'm wrong.

@suchipi
Copy link
Owner

suchipi commented May 28, 2022

Supporting a string key of "Unstarted | Error" is theoretically possible, since you can do some seriously wacky things with TypeScript, but it would be way too complicated for me to want to do :p

While it's not ideal, this is what I do for handling multiple cases:

// definition of LoadState and state omitted; they'd be the same as in your issue description

const unavailable = () => "unavailable";

console.log("State is ", state.match({
  Unstarted: unavailable,
  Error: unavailable,
  _: () => "available", 
}));

For something small like "unavailable" it looks silly, but when the match handler in question is larger, it makes more sense.

@TmLev
Copy link
Author

TmLev commented Jun 1, 2022

Seems reasonable albeit not very ergonomic. Thought about doing it this way, also. Thanks!

@TmLev TmLev closed this as completed Jun 1, 2022
@Evertt
Copy link

Evertt commented Sep 26, 2022

@suchipi If I, or anyone else for that matter, would offer you a PR that makes this feature possible, would you accept it?

@suchipi
Copy link
Owner

suchipi commented Sep 26, 2022

Yes, provided it was of good quality and etc

@suchipi
Copy link
Owner

suchipi commented Sep 26, 2022

Reopening to better communicate that interest

@suchipi suchipi reopened this Sep 26, 2022
@cuppachino
Copy link

cuppachino commented Feb 20, 2023

I don't know how much I like the suggested approach with "Unstarted" | Error, but the idea is really cool.

You might need something like PickAll and KeyOf from here to maintain type inference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants