-
-
Notifications
You must be signed in to change notification settings - Fork 198
Open
Description
Version Numbers
zodResolver
: 5.2.1
react-hook-form
: 7.62.0
zod
: 4.0.17
Codesandbox/Expo snack
StackBlitz (Codesandbox had bug)
Steps to reproduce
Observe this code snippet (or see link above):
import { zodResolver } from '@hookform/resolvers/zod';
import { ReactElement } from 'react';
import { useForm } from 'react-hook-form';
import { z } from 'zod';
const SubSchemaA = z.strictObject({
isA: z.literal(true),
});
const SubSchemaB = z.strictObject({
isA: z.literal(false),
});
const MySchema = z.union([SubSchemaA, SubSchemaB]);
export type MySchemaInput = z.input<typeof MySchema>;
interface FooProps {
defaultVals: MySchemaInput;
}
export function Foo({ defaultVals }: FooProps): ReactElement {
const { register } = useForm({
resolver: zodResolver(MySchema),
defaultValues: defaultVals,
});
return <h1>Zod Form</h1>;
}
Expected behaviour
This should have no type errors, but you get a type error on defaultValues
saying:
Type '{ isA: true; } | { isA: false; }' is not assignable to type 'AsyncDefaultValues<{ isA: true; }> | { isA?: true | undefined; } | undefined'.
It seems zodResolver
doesn't understand that isA
can be false
.
Code of Conduct
- I agree to follow this project's Code of Conduct
keywords: discriminating union
s0-david, juniorUsca, hmaesta, bernaferrari and mneira10
Metadata
Metadata
Assignees
Labels
No labels