Skip to content

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

@GringoDotDev

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!

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