Closed4

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

nbstshnbstsh
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;
}

nbstshnbstsh

解決

tsconfig で strict を true にすれば OK

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