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

Feature Request / RFC: export Enum type as object #54

Open
GringoDotDev opened this issue May 10, 2023 · 2 comments
Open

Feature Request / RFC: export Enum type as object #54

GringoDotDev opened this issue May 10, 2023 · 2 comments
Labels

Comments

@GringoDotDev
Copy link

Hi there,
This is a great package, and thank you so much for putting it together. I wanted to throw out an idea for feedback and see if it's something you might consider accepting as a PR if I were to put it together.

In summary, when generating enum types, we get output that looks something like this:

const CurrencyTypeEnum = {
  USD: 'USD',
  MXN: 'MXN',
  EUR: 'EUR',
} as const;

export type CurrencyTypeEnum = typeof CurrencyTypeEnum[keyof typeof CurrencyTypeEnum]

This works very well if all I need is the type definition, but what if I want to use the object directly - e.g. to populate a select box or something? As-is, I have to duplicate the object elsewhere in my code.

It would seem nicer to do something like this instead:

export const CurrencyTypeEnum = {
  USD: 'USD',
  MXN: 'MXN',
  EUR: 'EUR',
} as const;

And then adjust the references in the other typedefs to use the typeof of the object, e.g.:

export interface Listing {
...
  currency: typeof CurrencyTypeEnum|null
...
}

I currently solve this by using sed as part of my pipeline:

sed -i '' -e '/^export type .*Enum = typeof .*Enum\[keyof typeof .*Enum\]$/d' -e '/^const .*Enum =/{s//export &/;}' ./resources/js/generated-types.ts && \
sed -i '' 's/\(.*: \)\(.*Enum\)/\1typeof \2/' ./resources/js/generated-types.ts

But this is obviously a bit inelegant and brittle.

So on to my questions:

  • Is a change like this something you would accept into core?
  • Would we want to put it behind some sort of option/flag to preserve backwards compatibility?
  • Any pointers on where to look in the codebase to implement it?

Thank you!

@tcampbPPU
Copy link
Member

Hey @jseitel if I follow you would prefer if both Enum generated values could be exported, so per your example like:

export const CurrencyTypeEnum = {
  USD: 'USD',
  MXN: 'MXN',
  EUR: 'EUR',
} as const;

export type CurrencyTypeEnum = typeof CurrencyTypeEnum[keyof typeof CurrencyTypeEnum]

Yea I think adding that as an optional flag works, I'd be down to review a PR for that.

Some pointers on the codebase, is pretty much all starts with the ModelTyperCommand so here you can create your additional flag. Then everything else is a independent Action, so the action for this would be WriteEnumConst. So you just need to pass the CLI argument into that action.

@github-actions
Copy link

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

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

No branches or pull requests

2 participants