Z
Zod14mo ago
zerokelvin

zerokelvin - Why is MySchema.parse making optio...

Why is MySchema.parse making optional all of the properties of my schema? How do I stop it from doing this? Stackblitz: https://stackblitz.com/edit/typescript-pnkeqy?file=index.ts,package.json
import { z } from 'zod';

export const CreateMessage = z.object({
personId: z.number(),
fullName: z.string(),
email: z.string().email(),
});

const data = CreateMessage.parse({
personId: 1,
fullName: 'John Doe',
});

console.log(data);

/*
The type of "data" above is:
const data: {
personId?: number;
fullName?: string;
email?: string;
}


But instead it should be:

const data: {
personId: number;
fullName: string;
email: string;
}
*/
import { z } from 'zod';

export const CreateMessage = z.object({
personId: z.number(),
fullName: z.string(),
email: z.string().email(),
});

const data = CreateMessage.parse({
personId: 1,
fullName: 'John Doe',
});

console.log(data);

/*
The type of "data" above is:
const data: {
personId?: number;
fullName?: string;
email?: string;
}


But instead it should be:

const data: {
personId: number;
fullName: string;
email: string;
}
*/
StackBlitz
TypeScript Zod Playground (forked) - StackBlitz
Blank starter project for building TypeScript apps.
2 Replies
Sikari
Sikari14mo ago
This happens when you do not use strict: true in your tsconfig, more specifically strictNullChecks has to be enabled.
zerokelvin
zerokelvinOP14mo ago
Ah, that's it! That fixed my issue, thanks @Sikari !!
Want results from more Discord servers?
Add your server