pfgray / ts-adt

Generate Algebraic Data Types and pattern matchers
MIT License
316 stars 13 forks source link

Generalize discriminant field #8

Closed pfgray closed 3 years ago

pfgray commented 3 years ago

Right now, ADT fixes the discriminant field to _type. This works great, but it should be easy to generalize this, and allow users to supply their own discriminant field. potentially something like:

 MakeADT<D extends string, T extends Record<string, {}>> = {
  [K in keyof T]: Record<D, K> & T[K];
}[keyof T];

type Option<A> = MakeADT<'_tag', {
  some: { value: A },
  none: {}
}>

and now Option is:

type Option<A> = 
  | { _tag: "some", value: A }
  | { _tag: "none" }
broerjuang commented 3 years ago

awesome!