unsplash / sum-types

Safe, ergonomic, non-generic sum types in TypeScript.
https://unsplash.github.io/sum-types/
MIT License
42 stars 2 forks source link

Add refinement primitive #54

Closed samhh closed 1 year ago

samhh commented 1 year ago

Thinking of how best to tackle https://github.com/unsplash/sum-types-io-ts/pull/15 going forwards I landed here. In that specific case this could be utilised something like:

const unknownSerialize = flow(O.fromPredicate(Sum.is<A>()(k)(f)), O.map(Sum.serialize))
Magellol commented 1 year ago

Given that is requires knowing the key and the value ahead of time, I'm not sure how we can use it to solve https://github.com/unsplash/sum-types-io-ts/pull/15 because sum-types doesn't export accessors for the key and/or value.

// hypothetically
const unknownSerialize = (x: unknown): O.Option<readonly [unknown, unknown]> =>
  pipe(
    x,
    O.fromPredicate(Sum.is()(x[Sum.tagKey])((a: any): a is never => true)),
    O.map(Sum.serialize),
  )

Perhaps there is value of having an exported fn called isAny 🤔