colinhacks / zod

TypeScript-first schema validation with static type inference
https://zod.dev
MIT License
33.09k stars 1.15k forks source link

Bug: z.output makes all properties optional #3767

Closed MickL closed 4 hours ago

MickL commented 8 hours ago

Using z.output, ALL properties optional. E.g. the following should throw a TypeScript error that count is required but it does not:

export const myMsgSchema = z.object({
  message: z.string(),
  count: z.number().positive(),
});

export type MyMsg = z.output<typeof myMsgschema>;

const msg: MyMsg = {
  message: 'hello';
}
BrendanC23 commented 4 hours ago

Are you using TypeScript's strict mode? See the docs.

Also, your example has some small typos that might be causing issues.

export const myMsgSchema = z.object({
    message: z.string(),
    count: z.number().positive(),
});

export type MyMsg = z.output<typeof myMsgSchema>; // Fixed capitalization

const msg: MyMsg = {
    message: "hello", // Changed to comma
};

With strict mode, your example is giving me an error.

image

MickL commented 4 hours ago

The typos were because I created the example out of my code.

strict mode "fixed" it, thanks.