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

Type changes in 3.23 #3431

Closed
juliusmarminge opened this issue Apr 22, 2024 · 12 comments
Closed

Type changes in 3.23 #3431

juliusmarminge opened this issue Apr 22, 2024 · 12 comments

Comments

@juliusmarminge
Copy link

juliusmarminge commented Apr 22, 2024

Hi Colin,

@t3-oss/t3-env has started failing in zod@3.23: t3-oss/t3-env#222

We're not using any internal types which made me think you've unintentionally broken semver for this release?

See the source, we're basically just using ZodType:

We're also not really using the ZodType generics, so I don't think that change is relevant here. We're only using ZodType as a constraint

@colinhacks
Copy link
Owner

colinhacks commented Apr 22, 2024

Sorry about this. It's likely related to the changes to ZodType mentioned in the changelog (UPDATE: it's not, see below), though I also am surprised that it affected your package given how it uses ZodType. I couldn't immediately replicate this in the t3-env repo but based on t3-oss/t3-env#222 it seems like you've found a fix?

For others reading this here's the change:

- class ZodType<Output = any, Def extends ZodTypeDef = ZodTypeDef, Input = Output> {}
+ class ZodType<Output = unknown, Def extends ZodTypeDef = ZodTypeDef, Input = unknown> {}

This fixes what I consider to be a bug in ZodType that has caused a lot of headache for many many people, namely that it assumes Input = Output unless otherwise specified. This makes it unintuitive to use ZodType<T> in generic functions, or in conjunction with satisfies.

A side effect of this is that the inferred type of an unknown object schema is now { [k: string]?: unknown } instead of { [k: string]?: any }, which is a best practice.

@juliusmarminge
Copy link
Author

Yea seems like I had "fixed" it unintentionally since the last release. not sure what change did that though tbf, looks like it was just dependency bumps really...

@masterkain
Copy link

masterkain commented Apr 22, 2024

#15 20.73 ./lib/actions/projectActions.ts:137:47
#15 20.73 Type error: Argument of type 'Zod.ZodIssue[]' is not assignable to parameter of type 'import("/app/node_modules/zod-error/node_modules/zod/lib/ZodError").ZodIssue[]'.
#15 20.73   Type 'Zod.ZodIssue' is not assignable to type 'import("/app/node_modules/zod-error/node_modules/zod/lib/ZodError").ZodIssue'.
#15 20.73     Type 'ZodInvalidUnionIssue & { fatal?: boolean | undefined; message: string; }' is not assignable to type 'ZodIssue'.
#15 20.73       Type 'Zod.ZodInvalidUnionIssue & { fatal?: boolean | undefined; message: string; }' is not assignable to type 'import("/app/node_modules/zod-error/node_modules/zod/lib/ZodError").ZodInvalidUnionIssue & { fatal?: boolean | undefined; message: string; }'.
#15 20.73         Type 'ZodInvalidUnionIssue & { fatal?: boolean | undefined; message: string; }' is not assignable to type 'ZodInvalidUnionIssue'.
#15 20.73           Types of property 'unionErrors' are incompatible.
#15 20.73             Type 'Zod.ZodError<any>[]' is not assignable to type 'import("/app/node_modules/zod-error/node_modules/zod/lib/ZodError").ZodError<any>[]'.
#15 20.73               Type 'Zod.ZodError<any>' is not assignable to type 'import("/app/node_modules/zod-error/node_modules/zod/lib/ZodError").ZodError<any>'.
#15 20.73                 Types of property 'issues' are incompatible.
#15 20.73                   Type 'Zod.ZodIssue[]' is not assignable to type 'import("/app/node_modules/zod-error/node_modules/zod/lib/ZodError").ZodIssue[]'.
#15 20.73                     Type 'Zod.ZodIssue' is not assignable to type 'import("/app/node_modules/zod-error/node_modules/zod/lib/ZodError").ZodIssue'.
#15 20.73                       Type 'ZodInvalidArgumentsIssue & { fatal?: boolean | undefined; message: string; }' is not assignable to type 'ZodIssue'.
#15 20.73 
#15 20.73   135 |   const parsedData = formDataSchema.safeParse(formData);
#15 20.73   136 |   if (!parsedData.success) {
#15 20.73 > 137 |     const errorMessage = generateErrorMessage(parsedData.error.issues);
#15 20.73       |                                               ^
#15 20.73   138 |     revalidateTag(`project_${projectId}`);
#15 20.73   139 |     return { project_id: projectId, error: errorMessage };
#15 20.73   140 |   }

https://github.com/icoretech/airbroke/blob/3f8b7658c03ad2d03c73e6bcbc12633fc7d45135/lib/actions/projectActions.ts#L134-L140

I started seeing this in an automated build that proposes an upgrade to v3.23.0 from v3.22.5, I'll check it later but maybe it has something to do with this?

@paradoxloop
Copy link

paradoxloop commented Apr 22, 2024

@colinhacks this update has also broken typechecking on zodResolver of react-hooks-form in our projects.

Will do a deeper dive later if required but wanted to flag early.

@colinhacks
Copy link
Owner

colinhacks commented Apr 22, 2024

@masterkain That's a symptom of having two different versions of Zod installed in your project. The two ZodError classes aren't assignable to each other.

@paradoxloop I'd appreciate some kind of reproduction here, or some guidance on what you're seeing. I can't replicate this myself, and I checked for RHF resolver compatibility before publishing 3.23.

@colinhacks colinhacks changed the title type changes in 3.23 broke semver Type changes in 3.23 Apr 22, 2024
@jthrilly
Copy link

jthrilly commented Apr 22, 2024

I can't provide a full repro for the react-hooks-form issue, but I can share this helper hook that I believe is widely used (including in my projects) and is now erroring in the latest version:

import type { z } from 'zod';
import { useForm, type UseFormProps } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';

/**
 * Reusable hook for zod + react-hook-form
 */
export default function useZodForm<TSchema extends z.ZodType>(
  props: Omit<UseFormProps<TSchema['_input']>, 'resolver'> & {
    schema: TSchema;
  },
) {
  const form = useForm<TSchema['_input']>({
    ...props,
    resolver: zodResolver(props.schema, undefined),
  });

  return form;
}

Error:

Type 'TSchema["_input"]' does not satisfy the constraint 'FieldValues'

(FieldValues is typed as Record<string, any>).

Doing TSchema extends z.ZodType<any, z.ZodTypeDef, any> will obviously "get rid" of the error (albeit without proper input and output parsing). Its just an example of how people might've been using this previously.

I don't think its an issue per-se, but any advice on what to do with this scenario now would be appreciated by anyone who comes across this too.

@juliusmarminge
Copy link
Author

juliusmarminge commented Apr 22, 2024

@colinhacks
Copy link
Owner

Okay, given that this useZodForm hook is widely used, I think changing any to unknown is now too widely disruptive. I've reverted in 3.23.1.

@colinhacks
Copy link
Owner

colinhacks commented Apr 22, 2024

Upgrading to Zod 3.23.3 will resolve both the t3-env issue (see details) and React Hook Form issue!

@MaximeBernard
Copy link

@colinhacks We loved the change! Any timeline where this comes back?

@charIeszhao
Copy link

#15 20.73 ./lib/actions/projectActions.ts:137:47
#15 20.73 Type error: Argument of type 'Zod.ZodIssue[]' is not assignable to parameter of type 'import("/app/node_modules/zod-error/node_modules/zod/lib/ZodError").ZodIssue[]'.
#15 20.73   Type 'Zod.ZodIssue' is not assignable to type 'import("/app/node_modules/zod-error/node_modules/zod/lib/ZodError").ZodIssue'.
#15 20.73     Type 'ZodInvalidUnionIssue & { fatal?: boolean | undefined; message: string; }' is not assignable to type 'ZodIssue'.
#15 20.73       Type 'Zod.ZodInvalidUnionIssue & { fatal?: boolean | undefined; message: string; }' is not assignable to type 'import("/app/node_modules/zod-error/node_modules/zod/lib/ZodError").ZodInvalidUnionIssue & { fatal?: boolean | undefined; message: string; }'.
#15 20.73         Type 'ZodInvalidUnionIssue & { fatal?: boolean | undefined; message: string; }' is not assignable to type 'ZodInvalidUnionIssue'.
#15 20.73           Types of property 'unionErrors' are incompatible.
#15 20.73             Type 'Zod.ZodError<any>[]' is not assignable to type 'import("/app/node_modules/zod-error/node_modules/zod/lib/ZodError").ZodError<any>[]'.
#15 20.73               Type 'Zod.ZodError<any>' is not assignable to type 'import("/app/node_modules/zod-error/node_modules/zod/lib/ZodError").ZodError<any>'.
#15 20.73                 Types of property 'issues' are incompatible.
#15 20.73                   Type 'Zod.ZodIssue[]' is not assignable to type 'import("/app/node_modules/zod-error/node_modules/zod/lib/ZodError").ZodIssue[]'.
#15 20.73                     Type 'Zod.ZodIssue' is not assignable to type 'import("/app/node_modules/zod-error/node_modules/zod/lib/ZodError").ZodIssue'.
#15 20.73                       Type 'ZodInvalidArgumentsIssue & { fatal?: boolean | undefined; message: string; }' is not assignable to type 'ZodIssue'.
#15 20.73 
#15 20.73   135 |   const parsedData = formDataSchema.safeParse(formData);
#15 20.73   136 |   if (!parsedData.success) {
#15 20.73 > 137 |     const errorMessage = generateErrorMessage(parsedData.error.issues);
#15 20.73       |                                               ^
#15 20.73   138 |     revalidateTag(`project_${projectId}`);
#15 20.73   139 |     return { project_id: projectId, error: errorMessage };
#15 20.73   140 |   }

https://github.com/icoretech/airbroke/blob/3f8b7658c03ad2d03c73e6bcbc12633fc7d45135/lib/actions/projectActions.ts#L134-L140

I started seeing this in an automated build that proposes an upgrade to v3.23.0 from v3.22.5, I'll check it later but maybe it has something to do with this?

Had this error today in my project after upgrading the dependencies, but then I realized it was caused by having two different versions of zod in the node_modules. Removing the different version fixed the issue for me.

@asontha
Copy link

asontha commented May 14, 2024

+1 to the above, was migrating from 3.22.4 to 3.23.8 and needed to make sure all microservices using zod were on the same version.

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

8 participants