Skip to content

TypeScript · correctness

prefer-zod-infer

eslint:prefer-zod-infer

Derive a type from its Zod schema with `z.infer` instead of hand-writing a twin declaration beside it.

Default
error
Fix
none
Languages
typescript

Why

A derived type stays synchronized when the runtime schema changes.

Fix

Replace the hand-written twin with `z.infer<typeof Schema>`.

Before / after

Executed by this rule’s unit tests.

Before

Do not duplicate the schema shape

src/user.ts · focus
import { z } from "zod"; const UserSchema = z.object({ id: z.string() }); interface User { id: string }

After

Infer the schema type

src/user.ts · focus
import { z } from "zod"; const UserSchema = z.object({ id: z.string() }); type User = z.infer<typeof UserSchema>;

Message IDs

handWrittenTwin

Options

{
  "additionalProperties": false,
  "properties": {
    "ignoreTypeNames": {
      "items": {
        "type": "string"
      },
      "type": "array"
    },
    "requireIdenticalShape": {
      "type": "boolean"
    }
  },
  "type": "object"
}