unsplash / sum-types-io-ts

io-ts bindings for @unsplash/sum-types.
https://unsplash.github.io/sum-types-io-ts/
MIT License
2 stars 1 forks source link

More flexible non-nullary codecs #11

Closed samhh closed 1 year ago

samhh commented 2 years ago

Nullary sums now have getCodecFromStringlyMappedNullaryTag, but there's nothing equivalent for non-nullary sums if you don't want your data encoded specifically as Serialized.

Conversion functions like getCodecFromMappedNullaryTag, but with one or two supplied per member rather than across all members? How would this be handled in io-ts for non-primitive types?

samhh commented 2 years ago

It'd also be very useful to be able to decode structures like this:

{
  tag: 'MemberName',
  other: 'stuff',
  here: 'too',
}

To:

Sum.Member<'MemberName', { other: string; here: string }>

This may need to operate over entire objects like t.type.

Ideally it can also cover stuff like this:

{ tag: 'Human'; occupation: string } | { tag: 'Cat' }

And the tag needs to be flexible. Sometimes you might need to derive from a boolean property.

This is more complex than anything we already have. Maybe there's a layer of abstraction to discover first that's generalisable beyond sum-types.

samhh commented 1 year ago

Split out into #18, #19, #20, and #21.