Skip to content

Way to access tags as string constants from unionized [proposal] #75

@GrzegorzKazana

Description

@GrzegorzKazana

Hi 👋
first of all, thanks for this awesome utility library, I am having great time using it.

However, there is one use case which I think could be optimized. I find myself in situations, where I need to get access to the underlying string constants behind my actions/unions.

This leads me to write unions like this, which gets very boilerplateful and verbose pretty fast.

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

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

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

In perfect world, .is, .match and .transform should be enough, but when interfacing with specific libraries the need to access union tags is unavoidable. This in turn requires the user to sacrifice brevity.

Proposal

Store Record<tags, tags> inside the Unionized object.

Usage example:

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

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

I will be 100% willing and happy to prepare a PR with that feature, just let me know if you are interested in it and would like to include such functionality into the codebase.

Cheers 😄

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions