drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
24.68k stars 651 forks source link

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

Open ari-becker opened 1 year ago

ari-becker commented 1 year ago

Describe what you want

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

  1. Write a simple pgEnum:

image

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

image image

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

image

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

image

Not quite, the type parameter doesn't accept readonly. Let's find a trick on Stack Overflow:

image

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?

dankochetov commented 1 year ago
  1. why would you want to do that
  2. PgEnum<[string, ...string[]]>
ari-becker commented 1 year ago

@dankochetov that would be fine, but that also results in a type error :(

image

Angelelz commented 11 months ago

I'm also curious, isn't foo not typed correctly? Why do you want to annotate it?

ari-becker commented 11 months ago

I'm also curious, isn't foo not typed correctly? Why do you want to annotate it?

@Angelelz I've been bit by TS2742 ("the inferred type of X cannot be named without a reference to Y, this is likely not portable") a few too many times, and so I've developed a reflex to just add the type annotation when it's simple / brainless to do so. It just makes development smoother versus tripping over TS2742 later on and trying to figure out the type annotation later.