zod: TypeError: Not assignable to parameter of type ZodType
I’ve created this schema to use in React Hook Form, but I’m getting an error that I’m not sure how to fix.
import { z } from 'zod';
export const schema = z.object({
email_domains: z.string().min(1, { message: 'At least one domain is required' }),
add_to_groups: z.string(),
expires_at: z.string(),
language: z.string(),
name: z.string().min(1),
});
export type FormValuesType = z.infer<typeof schema>;
import { zodResolver } from '@hookform/resolvers/zod';
import { FormProvider, useForm } from 'react-hook-form';
import { defaultExpiresAt, defaultName } from '@/lib/invite-links';
import {
FormValuesType,
schema,
} from '@/components/InviteLinkCreatePage/schema';
export default function Form() {
const methods = useForm<FormValuesType>({
resolver: zodResolver(schema),
values: {
email_domains: '',
add_to_groups: '',
expires_at: defaultExpiresAt(),
language: '',
name: defaultName(),
},
});
const handleSubmit = (data: FormValuesType) => {
console.log({ data });
};
return (
<FormProvider {...methods}>
...
</FormProvider>
);
}
The error coming back is:
Argument of type 'ZodObject<{ email_domains: ZodString; add_to_groups: ZodString; expires_at: ZodString; language: ZodString; name: ZodString; }, "strip", ZodTypeAny, { ...; }, { ...; }>' is not assignable to parameter of type 'ZodType<any, any, any>'.
The types of 'refine(...)._def.typeName' are incompatible between these types.
Type 'import("/Users/ernstoarllano/Sites/dispel/packages/cweb/node_modules/zod/lib/types").ZodFirstPartyTypeKind.ZodEffects' is not assignable to type 'Zod.ZodFirstPartyTypeKind.ZodEffects'.
Property 'ZodReadonly' is missing in type 'Zod.ZodFirstPartyTypeKind'.
About this issue
- Original URL
- State: open
- Created 10 months ago
- Reactions: 41
- Comments: 35
Commits related to this issue
- chore(deps): update (#618) * chore(deps): update * chore: remove dependabot — committed to react-hook-form/resolvers by jorisre 10 months ago
- fix: downgrade Zod to v3.21.4 to workaround a build issue with RHF For more information, see: https://github.com/colinhacks/zod/issues/2663 — committed to arvl130/rrg-freight-services-web by arvl130 9 months ago
- build: Downgrade zod to 3.21.4 Fixes this issue: https://github.com/colinhacks/zod/issues/2663 — committed to KhaledElmorsy/blogfolio-client by KhaledElmorsy 3 months ago
@ernstoarllano Are you using a monorepo by chance? If so, are the versions for zod in all of your
package.jsons the same?This worked for me remove
node_modulesThanks! I needed
zodas a dependency so I just downgraded it to v3.21.4. I think that v3.22.0 and later are not compatible with some dependent libraries (like@hookform/resolvers) due toZodReadonly.The maintainer just released it. But I can confirm it does not fix the issue 😦 Same issue with zod@3.22.2 and @hookform/resolvers@3.3.0
3.22.4 not working for me as well
zod@3.21.4 and @hookform/resolvers: ^3.3.0 worked for me
zod@3.21.4 worked for me
In my case this is my code
I’m working with Zod 3.22.4, but still have the issue
This worked for me but wasn’t immediately apparent; I had to
Reload windowsin VS Code.Same, but why is it broken on the latest version
this is what worked here, after restarting vscode, thx
Same, but why is it broken on the latest version
workarround is downgrade to zod@3.21.4
thnks guys
I think a solution for zod@3.22.1 was just patched yesterday in
react-hook-form/resolvers. https://github.com/react-hook-form/resolvers/commit/e5e73466e0f2e098c022ef9c373d5fa82cea7306#diff-7ae45ad102eab3b6d7e7896acd08c427a9b25b346470d7bc6507b6481575d519L257-R256It has not been pushed to npm yet.
Updating to the latest zod 3.22.4 fixed it for me 👍
In case anyone else is being daft like me - I encountered this error in my monorepo (turborepo). I had forgotten to install zod to one of the places I was using it.
Zod v3.22.3 is out and I now get no linting errors 😊 Thanks!
I just remove the node modules and the lock file. That worked for me too
Developer Community Rocks 😃
Same issue I am now using zod@3.21.4 and @hookform/resolvers: ^3.1.0 Upgrade, reload vscode and it will work
Downgrading to 3.21.4 works for me, 3.22.4 does not.
3.22.4andrm -rf node_modulesdid not fix it for me.Downgrade to
3.21.4works.EDIT: I had a library using
3.21.4that was exposing a Zod type. Updating the library to3.22.4fixed it too. Can now use3.22.4.I’ve upgraded to Zod 3.22.3 and it seems to have fixed the issue (or at least changed the error message). With the example above, I now get the message
Type instantiation is excessively deep and possibly infinite.ts (2589)Thanks I got exactly the same error in
v3.22.1andv3.22.0and downgrading tov3.21.4resolved it