new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Zod · all subjects

type inference

8 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Inferring types from schemas with z.infer

Use the z.infer<> utility type to extract a static TypeScript type from a Zod schema. For example: type Player = z.infer<typeof Player>; extracts the inferred type from the Player schema.

Extracting output type from schema with z.output

Use z.output<typeof schema> to extract the output type of a schema. This is equivalent to z.infer<typeof schema> and is useful when the input and output types differ, such as when using .transform().

ZodObject key optionality logic will change for exactOptionalPropertyTypes in Zod 4

To enable exactOptionalPropertyTypes semantics, the logic used to determine key optionality in ZodObject will change in Zod 4. Depending on the value of exactOptionalPropertyTypes in your tsconfig.json, some inferred types may change.

Type safety difference between parse and decode/encode

.parse() and .decode() behave identically at runtime but have different type signatures. .parse() accepts unknown as input, while .decode() and .encode() have strongly-typed inputs. This allows TypeScript to catch type errors at compile time for decode and encode operations.

Extract TypeScript types from Zod schemas using z.infer

To extract a TypeScript type from a Zod schema, use z.infer<typeof SchemaName>. This produces a TypeScript type that matches the schema's parsed output. For example: type Person = z.infer<typeof Person> extracts the type from a Person schema.

z.input<> and z.output<> for preprocess types

For z.preprocess() schemas, use z.input<typeof schema> to get the input type (including the preprocessor's parameter type) and z.output<typeof schema> to get the output type (after preprocessing and validation). This is useful for libraries like react-hook-form that derive form value types from z.input<>.

brand() creates nominal types in structural TypeScript

Use .brand<"BrandName">() to add a brand to a schema's inferred type, simulating nominal typing. Example: const Cat = z.object({ name: z.string() }).brand<"Cat">() creates a type { name: string } & z.$brand<"Cat">. Branded types prevent unbranded data from being assignable to the branded type.

brand() only affects type, not runtime

Branded types do not affect the runtime result of .parse(). Branding is a static-only TypeScript construct.

Give your agent this brain