Skip to content
Zod logo

Zod

Officialv5.9.4

@kubb/plugin-zod

kubb add plugin-zod

Infer TypeScript types from Zod schemas

Set inferred to export a z.infer type next to each Zod schema, or z.input and z.output types for codecs, so you can skip @kubb/plugin-ts.

Turn on inferred types

With inferred: true, each schema gets a z.infer type named after it: the PascalCase schema name plus SchemaType, so petSchema gets PetSchemaType.

import { pluginZod } from '@kubb/plugin-zod'

pluginZod({ inferred: true })

To rename the types, see Prefix inferred types.

Type codecs on each side

With inferred: 'codec', Kubb writes z.input<typeof schema> for request schemas (bodies and params) and z.output<typeof schema> for response schemas.

  • Use it with codecs: a request takes the domain value, such as a Date, which the schema encodes, and a response returns the decoded value.
  • Clients and query hooks then take their operation types from these types instead of @kubb/plugin-ts.
  • 'direction' is the deprecated name for 'codec'.
With 'codec', set validator: { request: 'zod', params: 'zod' } in @kubb/plugin-fetch or @kubb/plugin-axios. Without request validation, the client sends the domain value unencoded.

When a printer handler returns a different expression per this.options.direction, Kubb also writes an ${name}InputSchema, which request bodies use, also through a $ref. Its type gets the InputSchemaType suffix:

src/gen/zod/slotSchema.ts
export type SlotSchemaType = z.output<typeof slotSchema>

export type SlotInputSchemaType = z.input<typeof slotInputSchema>

See Encode custom types for a full example.

Response types per status code

inferred also writes a ResponsesSchema for each operation, with one schema per status code, and its inferred type.

src/gen/zod/listPetsSchema.ts
export const listPetsResponsesSchema = z.object({
  '200': listPetsStatus200Schema,
})

export type ListPetsResponsesSchemaType = z.infer<typeof listPetsResponsesSchema>

@kubb/plugin-fetch and @kubb/plugin-axios key their RequestResult on it when @kubb/plugin-ts is absent.

Type guards with inferred types

With typeGuards, which needs Zod 4.6 or later, and inferred: true, the guards narrow to the generated type, such as PetSchemaType. With mini: true, they call z.validate and z.parse.

src/gen/zod/petSchema.ts
export const isPet = (data: unknown): data is PetSchemaType => petSchema.validate(data)