[BUG]: some filter definitions could use the addition of the `undefined` type

Author: lanpaiCreated Dec 10, 2024Updated Sep 16, 2026
Labelsbugpriorityqb/crud

Report hasn't been filed before.

  • I have verified that the bug I'm about to report hasn't been filed before.

What version of drizzle-orm are you using?

0.38.0

What version of drizzle-kit are you using?

0.30.0

Other packages

[email protected], [email protected], [email protected], @types/[email protected]

Describe the Bug

Currently a handful of types in query-builders/insert.d.ts have filters defined as where?: SQL or setWhere?: SQL instead of where?: SQL | undefined and setWhere?: SQL | undefined.

When using a stricter tsconfig that enables exactOptionalPropertyTypes, this is a faulty type since it fails when doing the following since conditionals like and and or are defined with a return type of SQL | undefined.

typescript
await db
  .insert(usersTable)
  .values({
    name: "lanpai",
    age: 1,
    email: "[email protected]",
  })
  .onConflictDoUpdate({
    target: usersTable.id,
    set: {
      age: 2,
      email: "[email protected]",
    },
    setWhere: and( // <- Type 'undefined' is not assignable to type 'SQL<unknown>'
      ne(usersTable.age, sql`excluded.age`),
      ne(usersTable.email, sql`excluded.email`),
    ),
  });

Source: drizzle-team/drizzle-orm