Skip to content

Temporal values lose their internal slots when a type with morphs clones its input #1719

Description

@LouisHaftmann

Report a bug

🔎 Search Terms

Temporal, clone, morph, internal slots, incompatible receiver

🧩 Context

  • ArkType version: 2.2.3 (@ark/util 0.56.2)
  • TypeScript version (5.1+): 6.0.3
  • Other context you think may be relevant (JS flags, envs, etc.): Node 26 and Chromium, both with native Temporal

🧑‍💻 Repro

import { type } from "arktype"

const T = type({ name: "string.trim", at: type.instanceOf(Temporal.Instant) })
const out = T.assert({ name: " x ", at: Temporal.Now.instant() })

out.at instanceof Temporal.Instant // true
out.at.toString()
// TypeError: Method Temporal.Instant.prototype.toString called on incompatible receiver [object Temporal.Instant]

The same happens for undeclared keys that are kept (default "+": "ignore"), and for every Temporal type (PlainDate, ZonedDateTime, Duration, ...). Without a morph (name: "string") the original instance passes through and works.

Cause

When a type has morphs, the input is cloned with _clone in @ark/util/out/clone.js. Temporal constructors are not in builtinConstructors, so a Temporal object is rebuilt with Object.create(Object.getPrototypeOf(input)) plus its own properties. Temporal objects keep their state in internal slots, so the copy passes instanceof but throws on every method call. The usual first sign is a serializer calling toString() and getting an error far away from validation.

Suggested fix

Return Temporal objects as-is in _clone, the same way other built-ins with internal state are returned. They are immutable, so sharing the reference is safe:

if (Object.prototype.toString.call(input).startsWith("[object Temporal."))
    return input

A more general option is to clone only plain objects (Object.prototype or null prototype) and arrays, and return any other object unchanged. Any class that keeps its state in private fields (#x) breaks the same way today.

Activity

  1. louisss1016 commented on Nov 4, 2026

    @louisss1016

    I'd like to work on this. I reproduced the incompatible receiver on current main with Node 26 and the public type(...).assert() API. I plan to preserve native Temporal values when cloning, with focused regression tests and the full pnpm prChecks run before opening a PR. This work is AI-assisted.

  2. moved this from To do to Done (merged or closed) in arktypeioon Nov 18, 2026
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

    Type

    No type

    Projects

    • Status
      Done (merged or closed)

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions