Skip to content

[BUG]: From beta release. Nested jsonb and array jsonb types aren't being inferred correctly using drizzle zod #4930

@kchasedevelopment

Description

@kchasedevelopment

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?

@beta

What version of drizzle-kit are you using?

@beta

Other packages

drizzle-zod@0.8.3, zod@4.1.9

Describe the Bug

Types aren't being inferred correctly from nested jsonb objects and jsonb arrays. This was working in non beta releases. With only 1 level deep it works, any deeper and its inferred as unknown.

// Array

export const ArraySchema = z.array(z.object({
  key: z.string(),
}));

export type ArraySchemaValues = z.infer<typeof ArraySchema>;

 array: jsonb()
 .$type<ArraySchemaValues>()
 .default(sql`'[]'::jsonb`)

const TableSchema = createInsertSchema(table)

type TableSchemaValues = z.infer<typeof TableSchema>

type Array = TableSchemaValues["array"]

// type inferred

type Array = {
    [x: number]: unknown;
    length: number;
    toString: unknown;
    toLocaleString: unknown;
    pop: unknown;
    push: unknown;
    concat: unknown;
    join: unknown;
    reverse: unknown;
    shift: unknown;
    slice: unknown;
    sort: unknown;
    splice: unknown;
    unshift: unknown;
    indexOf: unknown;
    lastIndexOf: unknown;
    every: unknown;
    some: unknown;
    forEach: unknown;
    map: unknown;
    filter: unknown;
    reduce: unknown;
    reduceRight: unknown;
    find: unknown;
    findIndex: unknown;
    fill: unknown;
    copyWithin: unknown;
    entries: unknown;
    keys: unknown;
    values: unknown;
    includes: unknown;
    flatMap: unknown;
    flat: unknown;
    at: unknown;
    findLast: unknown;
    findLastIndex: unknown;
    toReversed: unknown;
    toSorted: unknown;
    toSpliced: unknown;
    with: unknown;
    [Symbol.iterator]: unknown;
    [Symbol.unscopables]: unknown;
} | undefined

// Nested

export const NestedSchema = z.object({
  level1: z.object({
    level2: z.object({
      level3: z.string(),
    }),
  }),
});

export type NestedSchemaValues = z.infer<typeof NestedSchema>;

nested: jsonb()
  .$type<NestedSchemaValues>()
  .default(sql`'[]'::jsonb`)

const TableSchema = createInsertSchema(table)

type TableSchemaValues = z.infer<typeof TableSchema>

type Nested = TestSchemaValues["nested"];

// type inferred

type Nested = {
    level1: unknown;
} | undefined

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions