{"record":{"id":"7a73b8f44277b4f8","repo":"colinhacks/zod","slug":"invalid-return-type","errorCode":"invalid_return_type","errorMessage":"Invalid function return type","messagePattern":"Invalid function return type","errorType":"validation","errorClass":"ZodError","httpStatus":null,"severity":"error","filePath":"packages/zod/src/v3/types.ts","lineNumber":3897,"sourceCode":"            error.addIssue(makeReturnsIssue(result, e));\n            throw error;\n          });\n        return parsedReturns;\n      });\n    } else {\n      // Would love a way to avoid disabling this rule, but we need\n      // an alias (using an arrow function was what caused 2651).\n      // eslint-disable-next-line @typescript-eslint/no-this-alias\n      const me = this;\n      return OK(function (this: any, ...args: any[]) {\n        const parsedArgs = me._def.args.safeParse(args, params);\n        if (!parsedArgs.success) {\n          throw new ZodError([makeArgsIssue(args, parsedArgs.error)]);\n        }\n        const result = Reflect.apply(fn, this, parsedArgs.data);\n        const parsedReturns = me._def.returns.safeParse(result, params);\n        if (!parsedReturns.success) {\n          throw new ZodError([makeReturnsIssue(result, parsedReturns.error)]);\n        }\n        return parsedReturns.data;\n      }) as any;\n    }\n  }\n\n  parameters() {\n    return this._def.args;\n  }\n\n  returnType() {\n    return this._def.returns;\n  }\n\n  args<Items extends Parameters<(typeof ZodTuple)[\"create\"]>[0]>(\n    ...items: Items\n  ): ZodFunction<ZodTuple<Items, ZodUnknown>, Returns> {\n    return new ZodFunction({","sourceCodeStart":3879,"sourceCodeEnd":3915,"githubUrl":"https://github.com/colinhacks/zod/blob/2d90846af918af9602e088812d63a035d47cdbe4/packages/zod/src/v3/types.ts#L3879-L3915","documentation":"Thrown as a ZodError with code 'invalid_return_type' at packages/zod/src/v3/types.ts:3897 when a function wrapped by z.function() returns a value that fails the configured returns schema. After applying the implementation, the wrapper parses the result through the returns schema and surfaces any mismatch as this ZodError, protecting callers from contract violations inside the implementation.","triggerScenarios":"Defining `const fn = z.function().args(z.string()).returns(z.number()).implement((s) => s.length > 5)` where the implementation returns a boolean instead of a number; any case where the implement() body yields a value outside the declared return schema.","commonSituations":"The implementation has a bug and returns the wrong type (e.g. forgets to coerce, returns undefined on an early-return path); the returns schema was tightened but the body wasn't updated; async implementations that resolve to the wrong shape; third-party implementations that don't honour the contract.","solutions":["Read the ZodError.issues path to see which return constraint failed.","Fix the implementation so every code path returns a value matching the returns schema.","If the real return type legitimately differs, widen/narrow the returns schema to match reality.","For async implementations, ensure the returned Promise resolves (not rejects) to a conforming value and that implement() is awaited."],"exampleFix":"// before\nconst fn = z\n  .function()\n  .args(z.string())\n  .returns(z.number())\n  .implement((s) => s.length > 5); // returns boolean\n\n// after\nconst fn = z\n  .function()\n  .args(z.string())\n  .returns(z.number())\n  .implement((s) => s.length);","handlingStrategy":"try-catch","validationCode":"// Validate the implementation's return value during development.\nfunction verifyReturns(fnReturns: z.ZodType, impl: (...args: any[]) => unknown) {\n  return (...args: any[]) => {\n    const result = impl(...args);\n    const parsed = fnReturns.safeParse(result);\n    if (!parsed.success) {\n      throw new Error(`Implementation returned invalid value: ${parsed.error.message}`);\n    }\n    return parsed.data;\n  };\n}","typeGuard":"null","tryCatchPattern":"import { ZodError } from 'zod';\n\ntry {\n  fn('count');\n} catch (e) {\n  if (e instanceof ZodError && e.issues.some((i) => i.code === 'invalid_return_type')) {\n    // implementation broke its return contract\n    logReturnBug(e.issues);\n  } else {\n    throw e;\n  }\n}","preventionTips":["Treat invalid_return_type as a bug in the implementation, not a user error.","Make the returns schema loose enough to cover every code path in the implementation.","Cover all early-return branches in unit tests so the return contract is exercised.","For async implementations, ensure the resolved value (not the Promise itself) matches the returns schema."],"tags":["z-function","return-type","validation","runtime"],"backgroundTag":null,"analyzedSha":"2d90846af918af9602e088812d63a035d47cdbe4","analyzedAt":"2026-08-11T01:21:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}