Skip to content

Intersection throws "Unmergable intersection" when both sides produce the same NaN #6639

Open

Description

@andrewstellman

mergeValues decides two outputs are the same with a === b, which is false for NaN. When both sides of an intersection produce NaN, the merge is classed as unmergable and safeParse throws instead of returning a result:

import * as z from "zod";

z.nan().and(z.nan()).safeParse(NaN);          // throws: Unmergable intersection. Error path: []
z.unknown().and(z.unknown()).safeParse(NaN);  // throws
z.looseObject({ a: z.string() })
  .and(z.looseObject({ b: z.string() }))
  .safeParse({ a: "x", b: "y", c: NaN });     // throws, path ["c"]

The changelog reserves that throw for "an intersection of two incompatible types". Two NaNs from the same input aren't incompatible, and z.nan().and(z.nan()) currently can't accept any value. The compiled path reuses mergeValues, so it throws too.

Reproduced on main at 2bf7b06. A one-line fix, a === b || (Number.isNaN(a) && Number.isNaN(b)), with a test, is on a branch since PRs are limited to collaborators: main...andrewstellman:zod:intersection-nan-merge. 0 and -0 still merge as before (Object.is would have split them).

Found by Quality Playbook, an AI code-review tool, with Claude; I reviewed the change.

Activity

  1. anuj-hrcg commented on Oct 1, 2026

    @anuj-hrcg

    Could a maintainer review this independent fix and cherry-pick it if suitable? GitHub denied my account permission to create an upstream PR.

    Compare · Commit

    This implements andrewstellman’s proposed NaN equality condition while preserving 0/-0 behavior. Regressions fail before and pass afterward for sync/async parsing and nested object/array outputs; incompatible outputs still fail. Local object-intersection timings were approximately 209 ns before/after, retained schema memory was unchanged within measurement noise, and gzip bundle growth was 10 bytes for classic and 9 for Mini.

    All 8,323 tests, type checks, build, lint/format, and resolution/integration checks passed locally with Nub 0.8.3 and pinned Node 24.21.0; hooks were not bypassed. Performance numbers are local estimates, and hosted CI has not run. Prepared with AI assistance and verified locally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions