Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Not assignable to parameter of type ZodType<any, any, any> #2663

Open
ernstoarllano opened this issue Aug 16, 2023 · 38 comments
Open

Comments

@ernstoarllano
Copy link

ernstoarllano commented Aug 16, 2023

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'.
@masonbrothers
Copy link

I am having the same issue. I think it happened because I updated zod from 3.21.4 to 3.22.1

@masonbrothers
Copy link

masonbrothers commented Aug 16, 2023

@ernstoarllano Are you using a monorepo by chance? If so, are the versions for zod in all of your package.jsons the same?

@ernstoarllano
Copy link
Author

ernstoarllano commented Aug 16, 2023

@masonbrothers I am using a monorepo and it appears zod is already included. Removing the extra package did the trick. Was that the case for you as well?

@masonbrothers
Copy link

Thanks! I needed zod as 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 to ZodReadonly.

@MiryangJung
Copy link

Thanks! I needed zod as 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 to ZodReadonly.

Thanks I got exactly the same error in v3.22.1 and v3.22.0 and downgrading to v3.21.4 resolved it

@aimdexter
Copy link

I am having the same issue. Downgrading to v3.21.4 resolved it.

@masonbrothers
Copy link

I think a solution for zod@3.22.1 was just patched yesterday in react-hook-form/resolvers. react-hook-form/resolvers@e5e7346#diff-7ae45ad102eab3b6d7e7896acd08c427a9b25b346470d7bc6507b6481575d519L257-R256

It has not been pushed to npm yet.

jvandenaardweg referenced this issue in react-hook-form/resolvers Aug 22, 2023
* chore(deps): update

* chore: remove dependabot
@basilnewagesmb
Copy link

zod@3.21.4 worked for me

@jvandenaardweg
Copy link

I think a solution for zod@3.22.1 was just patched yesterday in react-hook-form/resolvers. react-hook-form/resolvers@e5e7346#diff-7ae45ad102eab3b6d7e7896acd08c427a9b25b346470d7bc6507b6481575d519L257-R256

It has not been pushed to npm yet.

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

@ernstoarllano
Copy link
Author

ernstoarllano commented Aug 23, 2023

In the context of a monorepo my team was able to fix it by adding a nohoist option to the proper workspace for @hookform/resolvers.

"workspaces": {
    "nohoist": [
      "@hookform/resolvers"
    ]
}

@jovanhartono
Copy link

zod@3.21.4 and @hookform/resolvers: ^3.3.0 worked for me

@jvandenaardweg
Copy link

In the context of a monorepo my team was able to fix it by adding a nohoist option to the proper workspace for @hookform/resolvers.


"workspaces": {

    "nohoist": [

      "@hookform/resolvers"

    ]

}

I don't understand why this fixes it? Although I have not tried your suggestion, all the related package versions in my monorepo have the same version. So this should not be needed, right?

@luc122c
Copy link

luc122c commented Aug 25, 2023

I'm still having this issue on v3.21.0 -> 3.22.1. I'm using Zod with VeeValidate in Nuxt. Example below:

const passwordSchema = z.object({
  currentPassword: z.string(),
  newPassword: z.string().min(8),
  confirmNewPassword: z.string().min(8),
});
const validationSchema = toTypedSchema(passwordSchema);

On the last line, I get a TS error on passwordSchema saying:

Argument of type 'ZodObject<{ currentPassword: ZodString; newPassword: ZodString; confirmNewPassword: ZodString; }, "strip", ZodTypeAny, { currentPassword: string; newPassword: string; confirmNewPassword: string; }, { ...; }>' is not assignable to parameter of type 'ZodType<any, ZodTypeDef, any>'.
  The types of 'refine(...)._def.typeName' are incompatible between these types. ts(2345)

I didn't have this error until recently, so I wonder if it is an issue with a related dependency?

@gabrielvenegas
Copy link

zod@3.21.4 and @hookform/resolvers: ^3.3.0 worked for me

this is working for me as well, version 3.22.2 of zod was not. Thanks! 🎉

@gsusmab
Copy link

gsusmab commented Sep 2, 2023

zod@3.21.4 and @hookform/resolvers: ^3.3.0 worked for me

Same here

@Nedi11
Copy link

Nedi11 commented Sep 2, 2023

zod@3.21.4 and @hookform/resolvers: ^3.3.0 worked for me

Same here

Same, but why is it broken on the latest version

@quyctd
Copy link

quyctd commented Sep 5, 2023

Same issue, currently using zod@3.22.2 and @hookform/resolvers: ^3.3.1

@christopheeyl
Copy link

Same issue, currently using zod@3.22.2 and @hookform/resolvers: ^3.3.1

Same issue I am now using zod@3.21.4 and @hookform/resolvers: ^3.1.0
Upgrade, reload vscode and it will work

@Anmol-Baranwal
Copy link

Developer Community Rocks :)

npm install zod@3.21.4
"@hookform/resolvers": "^3.3.1"

zod@3.21.4 and @hookform/resolvers: ^3.3.0 worked for me

@obrunofontana
Copy link

Same, but why is it broken on the latest version

workarround is downgrade to zod@3.21.4

thnks guys

@Leorrc
Copy link

Leorrc commented Sep 20, 2023

"zod": "^3.21.4"
"@hookform/resolvers": "^3.3.1"

this is what worked here, after restarting vscode, thx

arvl130 added a commit to arvl130/rrg-freight-services-web that referenced this issue Sep 23, 2023
@indahud
Copy link

indahud commented Sep 26, 2023

This worked for me
remove node_modules

pnpm store prune
pnpm install

@pedroamuniz
Copy link

I just remove the node modules and the lock file. That worked for me too

@ninoM
Copy link

ninoM commented Sep 29, 2023

This worked for me remove node_modules

pnpm store prune
pnpm install

This worked for me but wasn't immediately apparent; I had to Reload windows in VS Code.

@birdzai
Copy link

birdzai commented Oct 4, 2023

Zod v3.22.3 is out and I now get no linting errors 😊 Thanks!

@luc122c
Copy link

luc122c commented Oct 5, 2023

const passwordSchema = z.object({
  currentPassword: z.string(),
  newPassword: z.string().min(8),
  confirmNewPassword: z.string().min(8),
});
const validationSchema = toTypedSchema(passwordSchema);

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)

@traverse1984
Copy link

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.

@jvandenaardweg
Copy link

Updating to the latest zod 3.22.4 fixed it for me 👍

@hemagome
Copy link

hemagome commented Oct 7, 2023

In my case this is my code

const FormSchema = z.object({
firstname: z.string({
  required_error: "Nombre es requerido",
}).min(3, {
  message: "Nombre debe tener al menos 3 letras",
}),
lastname: z.string({
  required_error: "Apellidos son requeridos",
}).min(3, {
  message: "Nombre debe tener al menos 3 letras",
}),
cc: z.coerce.number({
  required_error: "Documento es requerido",
  invalid_type_error: "El documento debe ser numérico",
}).int().gte(10000000, {
  message: "Cedula debe tener al menos 8 digitos",
}).lte(9999999999, {
  message: "Cedula no puede tener más de 10 digitos",
}),
phone: z.coerce.number({
  required_error: "Teléfono requerido",
  invalid_type_error: "El teléfono debe ser numérico",
}).int().gte(3000000000).lte(3299999999),
contactPhone: z.coerce.number({
  required_error: "Teléfono requerido",
  invalid_type_error: "El teléfono debe ser numérico",
}).int().gte(3000000000).lte(3299999999),
contactName: z.string({
  required_error: "Nombre contacto es requerido",
}).min(3, {
  message: "Nombre debe tener al menos 3 letras",
}),
rh: z.string({
  required_error: "Por favor seleccione un grupo sanguíneo",
}).min(2).max(3),
birthdate: z.date({
  required_error: "Fecha de nacimiento es requerida",
}),
bio: z
  .string()
  .min(10, {
    message: "Información debe tener al menos 10 carácteres",
  })
  .max(160, {
    message: "Información no debe tener más de 160 carácteres",
  }),
eps: z.string({
  required_error: "Por favor seleccione una EPS",
}),
});

const form = useForm<z.infer<typeof FormSchema>>({
  resolver: zodResolver(FormSchema),
})

I'm working with Zod 3.22.4, but still have the issue

 Argument of type 'ZodObject<{ firstname: ZodString; lastname: ZodString; cc: ZodNumber; phone: ZodNumber; contactPhone: ZodNumber; contactName: ZodString; rh: ZodString; birthdate: ZodDate; bio: ZodString; eps: 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("C:/Users/hemag/OneDrive/Documents/GitHub/volt-riders/node_modules/.pnpm/zod@3.22.4/node_modules/zod/lib/types").ZodFirstPartyTypeKind.ZodEffects' is not assignable to type 'Zod.ZodFirstPartyTypeKind.ZodEffects'.
     Property 'ZodReadonly' is missing in type 'Zod.ZodFirstPartyTypeKind'.ts(2345)

@danilo-maida
Copy link

This worked for me remove node_modules

pnpm store prune
pnpm install

This definitely works even with latest packages version:

"zod": "^3.22.4"
"@hookform/resolvers": "^3.3.2",

@samal-rasmussen
Copy link

samal-rasmussen commented Oct 17, 2023

3.22.4 and rm -rf node_modules did not fix it for me.

Downgrade to 3.21.4 works.

EDIT: I had a library using 3.21.4 that was exposing a Zod type. Updating the library to 3.22.4 fixed it too. Can now use 3.22.4.

@Jaaneek
Copy link

Jaaneek commented Oct 19, 2023

Downgrading to 3.21.4 works for me, 3.22.4 does not.

@sonam-serchan
Copy link

3.22.4 not working for me as well

@rhmnaulia
Copy link

This worked for me remove node_modules

pnpm store prune
pnpm install

thanks, this works

@boian-ivanov
Copy link

My case was in a monorepo with zod types for an express API used with Sveltekit-Superforms where the package wouldn't have context on the zod schemas as they'd be any, but the v3.21.4 version throughout all packages worked wonders 🎉

@kevin1193
Copy link

"zod": "link:@hookform/resolvers/zod"

This works for me.

@mysterymosi
Copy link

**masonbrothers ** commented Aug 16, 2023

works thanks 🥲

@yu-shimada-altplus
Copy link

This happened to me with v3.24.0 and v3.24.1. I was able to avoid it by reverting to v3.23.8.
That helped, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests