Releases: pelotom/unionize
v1.0.0
v0.4.1
v0.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".
v0.3.0
v0.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
.