Closed4
zod の z.object() を infer すると optional になる問題

const FormSchema = z.object({
email: z.string(),
password: z.string(),
});
type FormValues = z.infer<typeof FormSchema>;
とすると、こんな感じ↓で email も password も optional になってしまう...
type FormValues = {
email?: string;
password?: string;
}

関連する issue あった

You have to use Zod with strict mode. This wasn't clear from the README - my bad. Just add "strict": true in your tsconfig's "compilerOptions" and it'll work properly.
tsconfig で strict が true になってないからか

解決
tsconfig で strict
を true にすれば OK
tsconfig.json
{
"compilerOptions": {
//...
+ "strict": true,
//...
}
}
このスクラップは2023/10/20にクローズされました