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

Comparison to https://github.com/practical-fp/union-types #23

Open
janwirth opened this issue Dec 3, 2021 · 1 comment
Open

Comparison to https://github.com/practical-fp/union-types #23

janwirth opened this issue Dec 3, 2021 · 1 comment

Comments

@janwirth
Copy link

janwirth commented Dec 3, 2021

  • I'm submitting a ...
    [x] question about the decisions made in the repository
    [x] question about how to use this project

  • Summary
    Why should I use this library over the other one?

@twop
Copy link
Owner

twop commented Dec 4, 2021

@janwirth , I haven't looked at https://github.com/practical-fp/union-types. It looks pretty awesome, thanks!

There are two things that came to my mind:

  1. union-types has transparent types for union values, e.g. you "know" that they have tag and value fields. While ts-union purposefully makes internal data structure opaque. Look at my comment here Ability to specify discriminator name #6 (comment) for more details.
  2. matchWith function that allows to match a tuple of unions. Even though I'm still not happy with the final API (hence experimental), I still think that it is a killer feature, at least for my use cases. It comes in handy if you are modeling transitions in Elm(ish) architecture pattern.

Here is the example from README

import { Union, of } from 'ts-union';

const State = Union({
  Loading: of(null),
  Loaded: of<number>(),
  Err: of<string>(),
});

const Ev = Union({
  ErrorHappened: of<string>(),
  DataFetched: of<number>(),
});

const { Loaded, Err, Loading } = State;

const transition = State.matchWith(Ev, {
  Loading: {
    ErrorHappened: (_, err) => Err(err),
    DataFetched: (_, data) => Loaded(data),
  },

  Loaded: {
    DataFetched: (loaded, data) => Loaded(loaded + data),
  },

  default: (prevState, ev) => prevState,
});

// usage
const newState = transition(Loading, Ev.ErrorHappened('oops')); // <-- State.Err('oops')

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

2 participants