8000 v4: Typescript compilation error - TS2636 Type 'ZodObject<sub-Shape, Extra>' is not assignable to type 'ZodObject<super-Shape, Extra>' · Issue #4178 · colinhacks/zod · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

v4: Typescript compilation error - TS2636 Type 'ZodObject<sub-Shape, Extra>' is not assignable to type 'ZodObject<super-Shape, Extra>' #4178

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

Closed
junknown opened this issue Apr 16, 2025 · 3 comments

Comments

@junknown
Copy link
junknown commented Apr 16, 2025

Compiling v4 ( 4.0.0-beta.20250415T232143 ) with typescript v5.8.3 and "skipLibCheck": false produce the following typescript error:

error TS2636: Type 'ZodInterface<sub-Shape, Params>' is not assignable to type 'ZodInterface<super-Shape, Params>' as implied by variance annotation.

@junknown junknown changed the title Typescript compilation error - TS2636 Type 'ZodObject<sub-Shape, Extra>' is not assignable to type 'ZodObject<super-Shape, Extra>' v4: Typescript compilation error - TS2636 Type 'ZodObject<sub-Shape, Extra>' is not assignable to type 'ZodObject<super-Shape, Extra>' Apr 16, 2025
@radiosilence
Copy link
radiosilence commented Apr 30, 2025

Update: Solved, I needed to use modules!

in package.json:

{
  "type": "module",
}

Working tsconfig.json for modules and node@23 (it has experimental TS support, you might have to do something a bit more for 22):

{
  "extends": [
    "@tsconfig/node23/tsconfig.json",
    "@tsconfig/node-ts/tsconfig.json"
  ],
  "compilerOptions": {
    "noEmit": true,
    "noImplicitReturns": false
  }
}

Also getting this error:

Type 'ZodObject<sub-Shape, Extra>' is not assignable to type 'ZodObject<super-Shape, Extra>' as implied by variance annotation.

 The types of '_zod.output' are incompatible between these types.
    Type '$InferObjectOutput<sub-Shape, Extra>' is not assignable to type '$InferObjectOutput<super-Shape, Extra>'.
      Type 'object | ({ [k in keyof (OptionalOutProps<sub-Shape> & RequiredOutProps<sub-Shape>)]: (OptionalOutProps<sub-Shape> & RequiredOutProps<sub-Shape>)[k]; } & Extra)' is not assignable to type '$InferObjectOutput<super-Shape, Extra>'.
        Type 'object' is not assignable to type '$InferObjectOutput<super-Shape, Extra>'.

Dependencies:

    "typescript": "5.8.3",
    "zod": "^4.0.0-beta.20250424T163858"

If there's any more info I can provide I happily will!

Here's a repo that replicates the error:

https://github.com/radiosilence/zod4-error

Trying a few different versions, 4.0.0-beta.1 is fine, 4.0.0-beta.20250409T222615 seems to be where it first breaks.

@Benjamin-Dobell
Copy link
Benjamin-Dobell commented May 2, 2025

There's additional issues if you don't have a global File type in your project (i.e. not including DOM or Node types). File is referenced but not defined.

[3:52:27 PM] File change detected. Starting incremental compilation...

node_modules/.pnpm/@zod+core@0.8.1/node_modules/@zod/core/dist/commonjs/checks.d.ts:256:55 - error TS2304: Cannot find name 'File'.

256 export interface $ZodCheckMimeTypeInternals<T extends File = File> extends $ZodCheckInternals<T> {
                                                          ~~~~

node_modules/.pnpm/@zod+core@0.8.1/node_modules/@zod/core/dist/commonjs/checks.d.ts:256:62 - error TS2304: Cannot find name 'File'.

256 export interface $ZodCheckMimeTypeInternals<T extends File = File> extends $ZodCheckInternals<T> {
                                                                 ~~~~

node_modules/.pnpm/@zod+core@0.8.1/node_modules/@zod/core/dist/commonjs/checks.d.ts:260:46 - error TS2304: Cannot find name 'File'.

260 export interface $ZodCheckMimeType<T extends File = File> extends $ZodCheck<T> {
                                                 ~~~~

node_modules/.pnpm/@zod+core@0.8.1/node_modules/@zod/core/dist/commonjs/checks.d.ts:260:53 - error TS2304: Cannot find name 'File'.

260 export interface $ZodCheckMimeType<T extends File = File> extends $ZodCheck<T> {
                                                        ~~~~

node_modules/.pnpm/@zod+core@0.8.1/node_modules/@zod/core/dist/commonjs/schemas.d.ts:725:62 - error TS2304: Cannot find name 'File'.

725 export interface $ZodFileInternals extends $ZodTypeInternals<File, File> {
                                                                 ~~~~

node_modules/.pnpm/@zod+core@0.8.1/node_modules/@zod/core/dist/commonjs/schemas.d.ts:725:68 - error TS2304: Cannot find name 'File'.

725 export interface $ZodFileInternals extends $ZodTypeInternals<File, File> {
                                                                       ~~~~

node_modules/.pnpm/zod@4.0.0-beta.20250420T053007_37a945f295484a6c262b17beba368b85/node_modules/zod/dist/commonjs/schemas.d.ts:410:31 - error TS2636: Type 'ZodInterface<sub-Shape, Params>' is not assignable to type 'ZodInterface<super-Shape, Params>' as implied by variance annotation.
  The types of '_zod.output' are incompatible between these types.
    Type '$InferInterfaceOutput<sub-Shape, Params>' is not assignable to type '$InferInterfaceOutput<super-Shape, Params>'.
      Type 'object | ({} extends sub-Shape ? object : { [k in keyof ({ -readonly [k in Params["optional"]]?: sub-Shape[k]["_zod"]["output"]; } & { -readonly [k in Exclude<keyof sub-Shape, Params["optional"]>]: sub-Shape[k]["_zod"]["output"]; } & Params["extra"])]: ({ -readonly [k in Params["optional"]]?: sub-Shape[k]["_zod"]["o...' is not assignable to type '$InferInterfaceOutput<super-Shape, Params>'.
        Type 'object' is not assignable to type '$InferInterfaceOutput<super-Shape, Params>'.

410 export interface ZodInterface<out Shape extends core.$ZodLooseShape = core.$ZodLooseShape, out Params extends core.$ZodInterfaceNamedParams = core.$ZodInterfaceNamedParams> extends ZodType {
                                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

node_modules/.pnpm/zod@4.0.0-beta.20250420T053007_37a945f295484a6c262b17beba368b85/node_modules/zod/dist/commonjs/schemas.d.ts:410:92 - error TS2636: Type 'ZodInterface<Shape, sub-Params>' is not assignable to type 'ZodInterface<Shape, super-Params>' as implied by variance annotation.
  The types of '_zod.output' are incompatible between these types.
    Type '$InferInterfaceOutput<Shape, sub-Params>' is not assignable to type '$InferInterfaceOutput<Shape, super-Params>'.
      Type 'object | ({} extends Shape ? object : { [k in keyof ({ -readonly [k in sub-Params["optional"]]?: Shape[k]["_zod"]["output"]; } & { -readonly [k in Exclude<keyof Shape, sub-Params["optional"]>]: Shape[k]["_zod"]["output"]; } & sub-Params["extra"])]: ({ -readonly [k in sub-Params["optional"]]?: Shape[k]["_zod"]["outpu...' is not assignable to type '$InferInterfaceOutput<Shape, super-Params>'.
        Type 'object' is not assignable to type '$InferInterfaceOutput<Shape, super-Params>'.
          Type '{} extends Shape ? object : { [k in keyof ({ -readonly [k in sub-Params["optional"]]?: Shape[k]["_zod"]["output"]; } & { -readonly [k in Exclude<keyof Shape, sub-Params["optional"]>]: Shape[k]["_zod"]["output"]; } & sub-Params["extra"])]: ({ -readonly [k in sub-Params["optional"]]?: Shape[k]["_zod"]["output"]; } & ....' is not assignable to type '{} extends Shape ? object : { [k in keyof ({ -readonly [k in super-Params["optional"]]?: Shape[k]["_zod"]["output"]; } & { -readonly [k in Exclude<keyof Shape, super-Params["optional"]>]: Shape[k]["_zod"]["output"]; } & super-Params["extra"])]: ({ -readonly [k in super-Params["optional"]]?: Shape[k]["_zod"]["output"...'.
            Type 'object | { [k in keyof ({ -readonly [k in sub-Params["optional"]]?: Shape[k]["_zod"]["output"]; } & { -readonly [k in Exclude<keyof Shape, sub-Params["optional"]>]: Shape[k]["_zod"]["output"]; } & sub-Params["extra"])]: ({ -readonly [k in sub-Params["optional"]]?: Shape[k]["_zod"]["output"]; } & ... 1 more ... & sub...' is not assignable to type '{} extends Shape ? object : { [k in keyof ({ -readonly [k in super-Params["optional"]]?: Shape[k]["_zod"]["output"]; } & { -readonly [k in Exclude<keyof Shape, super-Params["optional"]>]: Shape[k]["_zod"]["output"]; } & super-Params["extra"])]: ({ -readonly [k in super-Params["optional"]]?: Shape[k]["_zod"]["output"...'.
              Type 'object' is not assignable to type '{} extends Shape ? object : { [k in keyof ({ -readonly [k in super-Params["optional"]]?: Shape[k]["_zod"]["output"]; } & { -readonly [k in Exclude<keyof Shape, super-Params["optional"]>]: Shape[k]["_zod"]["output"]; } & super-Params["extra"])]: ({ -readonly [k in super-Params["optional"]]?: Shape[k]["_zod"]["output"...'.
                Type '{ [k in keyof ({ -readonly [k in sub-Params["optional"]]?: Shape[k]["_zod"]["output"]; } & { -readonly [k in Exclude<keyof Shape, sub-Params["optional"]>]: Shape[k]["_zod"]["output"]; } & sub-Params["extra"])]: ({ -readonly [k in sub-Params["optional"]]?: Shape[k]["_zod"]["output"]; } & ... 1 more ... & sub-Params["...' is not assignable to type '{ [k in keyof ({ -readonly [k in super-Params["optional"]]?: Shape[k]["_zod"]["output"]; } & { -readonly [k in Exclude<keyof Shape, super-Params["optional"]>]: Shape[k]["_zod"]["output"]; } & super-Params["extra"])]: ({ -readonly [k in super-Params["optional"]]?: Shape[k]["_zod"]["output"]; } & ... 1 more ... & supe...'.
                  Type 'super-Params["optional"] | Exclude<keyof Shape, super-Params["optional"]> | keyof super-Params["extra"]' is not assignable to type 'sub-Params["optional"] | Exclude<keyof Shape, sub-Params["optional"]> | keyof sub-Params["extra"]'.
                    Type 'super-Params["optional"]' is not assignable to type 'sub-Params["optional"] | Exclude<keyof Shape, sub-Params["optional"]> | keyof sub-Params["extra"]'.

410 export interface ZodInterface<out Shape extends core.$ZodLooseShape = core.$ZodLooseShape, out Params extends core.$ZodInterfaceNamedParams = core.$ZodInterfaceNamedParams> extends ZodType {
                                                                                               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

node_modules/.pnpm/zod@4.0.0-beta.20250420T053007_37a945f295484a6c262b17beba368b85/node_modules/zod/dist/commonjs/schemas.d.ts:457:28 - error TS2636: Type 'ZodObject<sub-Shape, Extra>' is not assignable to type 'ZodObject<super-Shape, Extra>' as implied by variance annotation.
  The types of '_zod.output' are incompatible between these types.
    Type '$InferObjectOutput<sub-Shape, Extra>' is not assignable to type '$InferObjectOutput<super-Shape, Extra>'.
      Type 'object | ({ [k in keyof (OptionalOutProps<sub-Shape> & RequiredOutProps<sub-Shape>)]: (OptionalOutProps<sub-Shape> & RequiredOutProps<sub-Shape>)[k]; } & Extra)' is not assignable to type '$InferObjectOutput<super-Shape, Extra>'.
        Type 'object' is not assignable to type '$InferObjectOutput<super-Shape, Extra>'.

457 export interface ZodObject<out Shape extends core.$ZodShape = core.$ZodShape, Extra extends Record<string, unknown> = Record<string, unknown>> extends ZodType {
                               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

@colinhacks
Copy link
Owner

Should be fixed in most circumstances in the latest betas.

The File thing is unrelated, feel free to open a new issue if you're still experiencing that. 👍

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

4 participants
0