pelotom / unionize

Boilerplate-free functional sum types in TypeScript
MIT License
402 stars 14 forks source link

Expose record tags as string constants #76

Open GrzegorzKazana opened 4 years ago

GrzegorzKazana commented 4 years ago

Adresses #75 .

Motivation When interfacing with other libraries/other code, there is sometimes a need for for using the tags from the tagged union. This results in need to define our unions as follows, which is unnecessarily verbose and boilerplateful.

const ON = 'ON';
const OFF = 'OFF';

const State = unionized({
  [ON]: ofType<{}>(),
  [OFF]: ofType<{}>()
})

// do something later with tag
console.log(ON)  // 'ON'

Solution Expose record tags as a property on the Unionized object.

const State = unionized({
  ON: ofType<{}>(),
  OFF: ofType<{}>()
})

// do something later with tag
console.log(State.tags.ON)  // 'ON'

/*
State.tags is:
{
  ON: 'ON';
  OFF: 'OFF';
}
*/
coveralls commented 4 years ago

Coverage Status

Coverage remained the same at 100.0% when pulling 046b830d20456da2b4453e19be375f4714e32054 on GrzegorzKazana:expose-tag-record into 55240dd4229528d050caac0ad6114bce7101fb78 on pelotom:master.