Skip to content

fix(transform): type standalone z.transform() ctx as $RefinementCtx#5766

Open
mahmoodhamdi wants to merge 1 commit intocolinhacks:mainfrom
mahmoodhamdi:fix/transform-ctx-addIssue-type
Open

fix(transform): type standalone z.transform() ctx as $RefinementCtx#5766
mahmoodhamdi wants to merge 1 commit intocolinhacks:mainfrom
mahmoodhamdi:fix/transform-ctx-addIssue-type

Conversation

@mahmoodhamdi
Copy link

What does this PR do?

Fixes the type of the ctx parameter in the standalone z.transform() factory function to include addIssue().

Root cause

The standalone z.transform((input, ctx) => ...) factory function typed ctx as ParsePayload, which only includes value, issues, and aborted. However, at runtime, the classic ZodTransform constructor mutates the payload to attach addIssue() before invoking the callback — so ctx.addIssue(...) works at runtime but TypeScript doesn't see it.

The chained .transform() method on ZodType already correctly types ctx as $RefinementCtx (which extends ParsePayload with addIssue). The standalone factory was just missed.

Fix

Changed the ctx parameter type from core.ParsePayload to core.$RefinementCtx<I> in the standalone transform() function signature.

Tests

Added two tests:

  • Runtime test: standalone z.transform() with ctx.addIssue() correctly surfaces validation errors
  • Type test: verifies ctx.addIssue is typed as a function via expectTypeOf

Related issue

Closes #5678

…ementCtx

The standalone z.transform() factory function typed the callback's ctx
parameter as ParsePayload, which doesn't include addIssue(). At runtime,
the classic ZodTransform constructor adds addIssue to the payload before
invoking the callback, so addIssue is always available. The chained
.transform() method on ZodType already correctly types ctx as
$RefinementCtx — this brings the standalone factory in line with it.

Closes colinhacks#5678
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

Successfully merging this pull request may close these issues.

transform callback ctx.addIssue not in Typescript

1 participant