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

Working with tagged unions #187

Closed
OliverJAsh opened this issue Jul 4, 2018 · 2 comments
Closed

Working with tagged unions #187

OliverJAsh opened this issue Jul 4, 2018 · 2 comments

Comments

@OliverJAsh
Copy link
Contributor

Related pelotom/unionize#41

For working with tagged unions, I'm a fan of unionize. Below I outline the pros and cons of working with tagged unions in both io-ts and unionize.

However, for all of unionize's benefits over io-ts, the one big thing missing is validation, which of course io-ts aims to provide.

By outlining the pros/cons of both approaches, my hope is that we can find a way to improve io-ts so that we can have the best of both worlds: concise syntax and validation.

Do you have any thoughts on this?

io-ts

// Pros:
// - `switch` can match multiple cases at once
// Cons:
// - Definition is verbose
// - Tag is required during construction
// - Renaming tags will not update all usages and the type definition

import * as t from 'io-ts';

// Definition

const FooRT = t.type({ tag: t.literal('Foo'), foo: t.number });
type Foo = t.TypeOf<typeof FooRT>;

const BarRT = t.type({ tag: t.literal('Bar') });
type Bar = t.TypeOf<typeof BarRT>;

const BazRT = t.type({ tag: t.literal('Baz') });
type Baz = t.TypeOf<typeof BazRT>;

const ModalRT = t.taggedUnion('tag', [FooRT, BarRT, BazRT]);
type Modal = t.TypeOf<typeof ModalRT>;

// Construction

ModalRT.encode({ tag: 'Foo', foo: 2 });
ModalRT.encode({ tag: 'Foo', foo: 'foo' });

// Matching

const matchModal = (modal: Modal) => {
    switch (modal.tag) {
        case 'Foo':
            return modal.foo;
        case 'Bar':
        case 'Baz':
            return 2;
    }
};

unionize

// Pros:
// - Renaming tags will update all usages and the type definition
// - Tag can be omitted during construction
// - Definition is concise
// Cons:
// - Unlike `switch`, can't match multiple cases at once

import { unionize, ofType } from 'unionize';

// Definition

const Modal = unionize({
    Foo: ofType<{ foo: number }>(),
    Bar: ofType<{}>(),
    Baz: ofType<{}>(),
});
type Modal = typeof Modal._Union;

// Construction

Modal.Foo({ foo: 2 });
Modal.Foo({ foo: 'foo' });

// Matching

const matchModal = Modal.match({
    Foo: ({ foo }) => foo,
    Bar: () => 2,
    Baz: () => 2,
});
@gcanti
Copy link
Owner

gcanti commented Jul 4, 2018

Do you have any thoughts on this?

Yes I do

@OliverJAsh
Copy link
Contributor Author

Brilliant, I knew you would 😉

This would be the perfect solution for us.

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