Skip to content

Temporal.PlainDate (and other Temporal types) throw on comparison #51

Description

@cn-stephen

Problem

Temporal.PlainDate (and the other Temporal.* types) intentionally throw when .valueOf() is called — they reject implicit numeric coercion by design:

TypeError: Do not use Temporal.PlainDate.prototype.valueOf;
use Temporal.PlainDate.prototype.compare for comparison.

microdiff's equality check on line 36 of dist/index.js uses +objKey === +newObjKey, which triggers .valueOf() and throws when either value is a Temporal type.

Reproduction

import diff from "microdiff";

const before = { date: Temporal.PlainDate.from("2024-03-15") };
const after  = { date: Temporal.PlainDate.from("2024-06-01") };

diff(before, after);
// → TypeError: Do not use Temporal.PlainDate.prototype.valueOf

Relevant code

// dist/index.js:34-36
!(areCompatibleObjects &&
    (isNaN(objKey)
        ? objKey + "" === newObjKey + ""   // string coercion — works
        : +objKey === +newObjKey))         // numeric coercion — throws for Temporal

Temporal.PlainDate is an object, typeof is "object", and it isn't in the richTypes allow-list, so the code reaches the +objKey path and explodes.

Possible fixes

  1. Add Temporal.PlainDate (and siblings) to richTypes so they get string-compared instead of numeric-compared.
  2. Wrap the +objKey coercion in a try/catch and fall back to string comparison.
  3. Check for a Symbol.toPrimitive or valueOf override that throws before attempting numeric coercion.

Option 1 is the smallest change but requires enumerating Temporal types. Option 2 is more resilient to future types that also reject .valueOf().

Context

Temporal is currently Stage 3 and shipping behind --harmony-temporal in V8 / Node.js 22+. It will become increasingly common as a Date replacement.

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