uni

unionize

Boilerplate-free functional sum types in TypeScript

Showing:

25 Versions

3.1.0

Expose TaggedRecord (#70)

3.0.2

Fixes bug with nested unions being untagged (https://github.com/pelotom/unionize/issues/61)

3.0.1

Fix no-arg check to avoid matching objects with all optional properties (#48)

3.0.0

Ban TagProp as a usable property in variants when no ValProp is used (#56). This is technically a breaking change but probably won't break code that didn't have latent bugs.

2.2.0

Fix for TypeScript 3.4 when using arrays for values:

const Foo = unionize({ array: ofType<boolean[]>() }, { value: 'value' });

// no type error in unionize@<=2.1.2 under typescript@3.4.1:
Foo.array('whoops');

2.1.2

Fixes #39

2.1.1

Fixes #34

2.1.0

Allow including tags in variant types, so that you can name your variants with interfaces:

interface Foo {
  tag: 'foo';
  x: number;
}

interface Bar {
  tag: 'bar';
  y: boolean;

const T = unionize({
  foo: ofType<Foo>(),
  bar: ofType<Bar>(),
});

2.0.0

Breaking changes
  • A new API for specifying the default case (using key default in the cases object) (#19)
  • Specify tag and value property names via a config object instead of positional arguments (#22)
Other enhancements
  • Uncurried variants of match (#21)
  • A simplified version of match, transform, for creating functions of the union type to itself (#23)
  • Creation functions for variants of value type {} no longer require an argument (#31)
  • Hygienic type operators UnionOf, RecordOf, TagsOf for extracting the types associated with a unionized object (#32)
Bug fixes
  • Fix issue where _Record wasn't using the actual object that was passed in (#20)

See the README for more details. Thanks to @twop for his significant contributions to this release!

1.0.1

Fixes #16.

1.0.0

Improved match performance.

0.4.1

Adds support for avoiding conflicts where possible between tag name and underlying representation when using merged unions.

0.4.0

Typecasting via as

Downcast an element of a union to a specific variant:

const { id, text } = Action.as.ADD_TODO(someAction) // throws if someAction is not an ADD_TODO
Payload properties are merged by default

By default now, the "payload" of a union variant is not put into a separate field but is merged into the same object that contains the tag. That is, instead of having variants of the form

{ tag: 'foo'; value: { x: number; y: boolean } }

we have

{ tag: 'foo'; x: number; y: boolean }

by default. This is strictly more general in what it can express. You can still recover the old behavior by specifying a valProp when you call unionize. The reason for this change is that it makes it much nicer to use unionize to define props for React components in which "invalid states are unrepresentable".

0.3.0

The unionize function has been overloaded to allow providing optional tagProp and valProp arguments, supersedingunionizeCustom which is no longer exported.

0.2.0

Fixed #3 by changing the API to require a value of the record type to be passed in when unionizing. As an example, what was before

const Foo = unionize<{
  x: number
  y: boolean
}>()

becomes

const Foo = unionize({
  x: ofType<number>(),
  y: ofType<boolean>(),
})

Slightly more verbose, but it has the advantage of being faster, more universally supported, and because the tags are now enumerable you can do things like directly pass the result of a unionize call to Redux' bindActionCreators.

0.1.0

0.0.9

0.0.8

0.0.7

0.0.6

0.0.5

0.0.4

0.0.3

0.0.2

0.0.1