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

Add enumType option that matches Prisma client's enum definitions #6

Merged
merged 3 commits into from
Aug 26, 2024

Conversation

arithmetric
Copy link
Contributor

As described in #5, this generator's output for enums does not match the enums defined in the Prisma client. This can cause TypeScript errors due to mismatching types.

This PR adds a new object value to the "enumType" option that generates const and type definitions for enums to match Prisma Client.

For example, if an enum is defined in Prisma’s schema file like this:

enum UserStatus {
  ACTIVE
  BLOCKED
  PENDING
}

In the Prisma client (index.d.ts), the enum is defined as:

export namespace $Enums {
  export const UserStatus: {
  ACTIVE: 'ACTIVE',
  BLOCKED: 'BLOCKED',
  PENDING: 'PENDING'
};

export type UserStatus = (typeof UserStatus)[keyof typeof UserStatus]

With the enumType option value "object", this generator produces the following output:

export const UserStatus = {
  ACTIVE: "ACTIVE",
  BLOCKED: "BLOCKED",
  PENDING: "PENDING"
};

export type UserStatus = (typeof UserStatus)[keyof typeof UserStatus];

@mogzol
Copy link
Owner

mogzol commented Aug 26, 2024

As mentioned in my comment on your issue, the exported type should be identical to the exported type when using stringUnion. Still, the new object type you've added is nice because it lets you use the const export like an enum to access the values, while avoiding the compatibility issues caused by using typescript enums. And since the type is identical to the stringUnion types, it could even be made the default without breaking backwards compatibility.

I will try to merge this and create a new release later today.

@mogzol mogzol merged commit 437b8aa into mogzol:main Aug 26, 2024
1 check passed
@mogzol
Copy link
Owner

mogzol commented Aug 27, 2024

Just realized that the types exported will always just be "string" due to type widening. I just pushed a change to add as const, so that the types will be the proper union. See 704b819

@mogzol
Copy link
Owner

mogzol commented Aug 27, 2024

This is now published as v1.5.0. Thanks for the PR!

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

Successfully merging this pull request may close these issues.

2 participants