drizzle-team / drizzle-team/drizzle-orm

[BUG]: TypeScript Error: Type 'true' is not assignable to type 'never' when using .omit() in drizzle-zod

Open
#4,453 3 comments 10 reactions 0 assignees View on GitHub
bug drizzle/zod
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

### Report hasn't been filed before.

- [x] 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.42.0

### What version of `drizzle-kit` are you using?

^0.31.0

### Other packages

_No response_

### Describe the Bug

**Undesired behavior:**
When using the .omit() method from drizzle-zod on a schema created with `createInsertSchema()`, TypeScript incorrectly reports an error: `Type 'true' is not assignable to type 'never'`.

**Steps to reproduce:**

1. Create a table schema with drizzle-orm
2. Use `createInsertSchema()` to create a Zod schema from the table
3. Try to use `.omit() `to remove fields from the schema
4. TypeScript will show an error on the property values in the omit object

**Desired result:**
The `.omit()` method should properly accept `{ fieldName: true }` format without TypeScript errors, matching the Zod API.

**Reproduction code:**

```Typescript
import {
integer,
pgTable,
varchar,
text,
doublePrecision,
} from "drizzle-orm/pg-core";
import { createInsertSchema } from "drizzle-zod";
import { z } from "zod";

// Define table schema
export const productsTable = pgTable("products", {
id: integer().primaryKey().generatedAlwaysAsIdentity(),
name: varchar({ length: 255 }).notNull(),
description: text(),
image: varchar({ length: 255 }),
price: doublePrecision().notNull(),
});

// This produces a TypeScript error:
// Error: Type 'true' is not assignable to type 'never'
export const createProductSchema = createInsertSchema(productsTable).omit({
id: true, // Error occurs here
});

// This also produces the same error:
export const updateProductSchema = createInsertSchema(productsTable)
.omit({
id: true, // Error occurs here
})
.partial();
```

**Current workaround:**
I've worked around this by defining schemas directly with Zod instead:

```Typescript
export const createProductSchema = z.object({
name: z.string(),
description: z.string().optional().nullable(),
image: z.string().optional().nullable(),
price: z.number(),
});

export const updateProductSchema = z.object({
name: z.string().optional(),
description: z.string().optional().nullable(),
image: z.string().optional().nullable(),
price: z.number().optional(),
});

**Additional information:**

- Database engine: PostgreSQL
- TypeScript version: 5.8.3
- tsconfig.json relevant settings:

```Typescript
{
"compilerOptions": {
"module": "ESNext",
"target": "ES2020",
"moduleResolution": "node",
"esModuleInterop": true,
"strict": true
}
}
```

I believe this is an issue with the type definitions in drizzle-zod, specifically how the generic type parameters are defined for the `.omit()` method. The error suggests that the implementation expects a different type than what's being passed.

I'd be happy to contribute to fixing this issue if needed. Please let me know how I can help.

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.