Skip to content

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

Open

Description

@GringoDotDev

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!

Activity

  1. tcampbPPU commented on May 10, 2023

    @tcampbPPU
    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.

  2. github-actions commented on Jul 29, 2023

    @github-actions

    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.

  3. Rod911 commented on Jun 24, 2025

    @Rod911

    Yes please, need this

  4. github-actions commented on Jul 25, 2025

    @github-actions

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions