Skip to content

Return null from Date.toJSON when the time is not finite - #935

Open
phall1 wants to merge 1 commit into
vercel-labs:mainfrom
phall1:fix/date-tojson-null
Open

phall1 wants to merge 1 commit into
vercel-labs:mainfrom
phall1:fix/date-tojson-null

Conversation

@phall1

@phall1 phall1 commented Oct 13, 2026

Copy link
Copy Markdown

Date.prototype.toJSON had no lowering (SC2020). Node returns null when the time value is not finite, and otherwise the same string as toISOString. toISOString still throws RangeError.

JSON.stringify was also narrowing a string | null union to the checker's string before walking it, so the null result threw instead of printing null. A union that is already JSON-stringify-safe now keeps every arm. A union that is not JSON-safe still narrows.

const invalid = new Date(NaN);
JSON.stringify(invalid.toJSON()); // "null"
invalid.toJSON() === null; // true
invalid.toISOString(); // throws RangeError

A finite date prints the same ISO string from both methods:

const date = new Date("2024-01-01T12:00:00Z");
JSON.stringify([date.toJSON(), date.toISOString()]);
// ["2024-01-01T12:00:00.000Z","2024-01-01T12:00:00.000Z"]

Differential test: packages/compiler/test/date-tojson.test.ts. Native stdout, stderr, and exit match Node.

Before this change both cases failed to compile:

Date.prototype.toJSON is part of the standard library types but has no scriptc lowering yet (SC2020).

This showed up while compiling Effect 4. The public harness is https://github.com/phall1/effect-scriptc-compat. The change is the Date method, not an Effect workaround. Date values themselves are still not JSON-safe objects, and a binding the checker types as string can still reject the null result, because TypeScript types Date.toJSON() as string.

toISOString still throws RangeError. JSON.stringify keeps a json-safe
string | null union instead of narrowing the null arm away.
@vercel

vercel Bot commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

Someone is attempting to deploy a commit to the Vercel Labs Team on Vercel.

A member of the Team first needs to authorize it.

This branch has not been deployed

No deployments
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.

1 participant