Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
GrzegorzKazana opened this issue Sep 26, 2020 · 1 comment
Open

Comments

@GrzegorzKazana
Copy link

GrzegorzKazana commented Sep 26, 2020

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 😄

@lorefnon
Copy link
Contributor

lorefnon commented Jan 30, 2022

I also think this can be convenient. Can this PR please be merged ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants