drizzle-team / drizzle-team/drizzle-orm

[FEATURE]: Improve ergonomics of type-safety of PgEnum

Open
#1,540 5 comments 0 reactions 0 assignees View on GitHub
db/postgres enhancement
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

### Describe what you want

Here's the user story of writing a new `PgEnum`:

1. Write a simple `pgEnum`:

![image](https://github.com/drizzle-team/drizzle-orm/assets/1388505/a41e7c9f-180f-444a-ac81-f9ec5fc63e64)

Cool, simple, works, compiles. But the type is only inferred here, rather than checked / enforced. So let's try to add a `satisfies`:

![image](https://github.com/drizzle-team/drizzle-orm/assets/1388505/15c944fb-f07f-4c65-a5a5-da86ef99f225)
![image](https://github.com/drizzle-team/drizzle-orm/assets/1388505/6c0fac18-657f-46cd-a7b8-f4dd2970d4fe)

Ah, `PgEnum` requires a type parameter. Let's add one:

![image](https://github.com/drizzle-team/drizzle-orm/assets/1388505/0795b92f-9dc6-4bcc-81c5-9569c92f110b)

Green again. But now I've duplicated the enum values into both the enum definition and into the type-check. Can we DRY?

![image](https://github.com/drizzle-team/drizzle-orm/assets/1388505/f5318fa8-ca81-457f-bc86-a94487269b81)

Not quite, the type parameter doesn't accept `readonly`. Let's find [a trick on Stack Overflow](https://stackoverflow.com/a/43001581):

![image](https://github.com/drizzle-team/drizzle-orm/assets/1388505/56e625f9-20ff-42b5-9a86-15bf82245cfb)

Great, back to green. But this feels very awkward / boilerplate...? Is it not possible to have either `satisfies PgEnum;` (similar to `PgTable`), without specifying a type parameter, or have the type parameter accept `readonly` so that it can use `typeof` an `as const` array?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.