Skip to content

Add lib types for JSON.rawJSON, JSON.isRawJSON, and reviver context - #63248

Closed
Vedant Madane (VedantMadane) wants to merge 1 commit into
microsoft:mainfrom
VedantMadane:feat/json-parse-with-source
Closed

Vedant Madane (VedantMadane) wants to merge 1 commit into
microsoft:mainfrom
VedantMadane:feat/json-parse-with-source

Conversation

@VedantMadane

@VedantMadane Vedant Madane (VedantMadane) commented Mar 13, 2026 •

Copy link
Copy Markdown

Adds es2025.json.d.ts with type definitions for the Stage 4 TC39 proposal-json-parse-with-source which is part of ES2025 and has shipped in all major browsers and Node.js 22+. ## Summary - JSON.rawJSON(text) - creates a frozen RawJSON object for lossless JSON serialization (e.g. preserving large-integer precision) - JSON.isRawJSON(value) - type-guard that narrows to RawJSON - JSON.parse reviver context - overload whose reviver receives a third parameter { source: string } containing the original JSON source text of each primitive value ### New files | File | Purpose | |---|---| | src/lib/es2025.json.d.ts | RawJSON interface + JSON augmentations | | tests/cases/compiler/jsonParseWithSource.ts | Compiler test exercising all three APIs | | tests/baselines/reference/jsonParseWithSource.* | Accepted baselines | ### Modified files | File | Change | |---|---| | src/lib/es2025.d.ts | /// <reference lib=es2025.json /> | | src/lib/libs.json | Register es2025.json | | src/compiler/commandLineParser.ts | Map es2025.json to its .d.ts | Fixes #61330

@Renegade334

Copy link
Copy Markdown
Contributor

which is part of ES2025

It isn't, MDN is wrong on this one. It has stage 4 approval, but is not yet in the latest draft: tc39/ecma262#3714

@typescript-bot

Copy link
Copy Markdown
Contributor

With 6.0 out as the final release vehicle for this codebase, we're closing all PRs that don't fit the merge criteria for post-6.0 patches. If you think this was a mistake and this PR fits the post-6.0 patch criteria, please post to the 6.0 iteration issue with details (specifically, which PR and which patch criteria it satisfies).

Next steps for PRs:

  • For crash bugfixes or language service improvements, PRs are currently accepted at the typescript-go repo
  • Changes to type system behavior should wait until after 7.0, at which point mainline TypeScript development will resume in this repository with the Go codebase
  • Library file updates (lib.d.ts etc) continue to live in this repo or the DOM Generator repo as appropriate

@github-project-automation github-project-automation Bot moved this from Not started to Done in PR Backlog Mar 24, 2026
@RyanCavanaugh

Copy link
Copy Markdown
Member

Reopening as this is still valid (minus commandlineParser.ts)

ArshVermaGit

This comment was marked as spam.

Copilot AI review requested due to automatic review settings April 16, 2026 07:39
@typescript-bot TypeScript Bot (typescript-bot) added the For Backlog Bug PRs that fix a backlog bug label Apr 16, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an ES2025-by-feature lib file to type the now-standard JSON.rawJSON, JSON.isRawJSON, and the JSON.parse reviver “source context” capability, and wires it into the es2025 lib plus compiler test coverage.

Changes:

  • Introduces src/lib/es2025.json.d.ts defining RawJSON and augmenting JSON with rawJSON, isRawJSON, and a JSON.parse overload with a context parameter.
  • References the new lib from src/lib/es2025.d.ts and registers es2025.json in src/lib/libs.json.
  • Adds a compiler test (jsonParseWithSource.ts) and accepted baselines covering all three APIs.

Reviewed changes

Copilot reviewed 5 out of 7 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/lib/es2025.json.d.ts Adds RawJSON and JSON API typings for the ES2025 JSON additions.
src/lib/es2025.d.ts Includes the new es2025.json lib in the es2025 umbrella lib.
src/lib/libs.json Registers es2025.json as an available by-feature lib.
tests/cases/compiler/jsonParseWithSource.ts Compiler test exercising rawJSON, isRawJSON, and reviver context.
tests/baselines/reference/jsonParseWithSource.types Baseline for type printing of the new APIs.
tests/baselines/reference/jsonParseWithSource.symbols Baseline for symbol resolution including the new lib.
tests/baselines/reference/jsonParseWithSource.js Baseline for JS emit.

@afurm Andrii Furmanets (afurm) left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approach / Design (nits):

  1. JSON.rawJSON(text: string): RawJSON — The JSDoc documents the happy path but doesn't mention @throws for invalid input. Per the spec, JSON.rawJSON throws a SyntaxError when text is not a valid JSON primitive (not a string, number, boolean, or null) or is an object/array. Adding @throws {SyntaxError} would align with the existing JSON.parse JSDoc and help consumers handle error cases.

  2. Test coverage for reviver context — The test file has one reviver test using a plain object. It might be worth adding cases for:

    • Array elements ("arr": [1, 2, 3]) — the context source for each element
    • Nested objects to verify the context is available at all levels

These are minor suggestions; the type definitions themselves look correct and match the Stage 4 proposal. The infrastructure changes (libs.json, commandLineParser.ts, es2025.d.ts) all look routine and correct.

@RyanCavanaugh

Copy link
Copy Markdown
Member

Andrii Furmanets (@afurm) future automated comments will result in a block; any automated activity we want to occur in this repo we will set up ourselves or already have

@MulverineX

Copy link
Copy Markdown

Ryan Cavanaugh (@RyanCavanaugh) the commandLineParser.ts has since been removed, is there a reason this PR is not proceeding?

Comment thread src/lib/es2025.json.d.ts Outdated
Vedant Madane (VedantMadane) added a commit to VedantMadane/TypeScript that referenced this pull request Aug 8, 2026
Per the ECMAScript spec, context.source is only present when the property is unmodified and its value is primitive. Update the es2025.json typings and compiler tests/baselines accordingly.

Also re-add es2025.json to libMap so /// <reference lib="es2025.json" /> resolves (required for the es2025 umbrella lib to include the definitions).

Addresses review feedback on microsoft#63248.
@RyanCavanaugh Ryan Cavanaugh (RyanCavanaugh) added the Voight-Kampff Anomaly Repeated exogenous PRs look increasingly conceivable as non-human tinkering label Aug 10, 2026
@VedantMadane
Vedant Madane (VedantMadane) force-pushed the feat/json-parse-with-source branch 2 times, most recently from 34237c8 to 38e8ed5 Compare August 24, 2026 10:14
@MulverineX

Copy link
Copy Markdown

Even if this is a bot this is still something that needs to get implemented, its pretty annoying to have to vendor these types in

Port proposal-json-parse-with-source (ES2025) lib typings onto the Go
tree layout:

- tsc/internal/bundled/source/es2025.json.d.ts (+ generated lib/embed)
- reference from es2025 umbrella + libs.json + LibMap (enummaps.go)
- compiler test and baselines

Preserves RawJSON, JSON.rawJSON, JSON.isRawJSON, and optional
context.source on JSON.parse reviver.

Fixes microsoft#61330

Signed-off-by: Vedant Madane <6527493+VedantMadane@users.noreply.github.com>
@VedantMadane

Copy link
Copy Markdown
Author

Rebased onto current main and resolved merge conflicts.

New head: 3e03cf29a24aaa117f029296c9f7786c7eb81146

Conflict resolution

File Resolution
tsc/internal/tsoptions/enummaps.go Kept main's es2026.* lib map entries and re-added es2025.json
.../Parse invalid option of library flags.js Took main's error text (includes es2026 libs) and inserted 'es2025.json' after 'es2025.regexp'

Regenerated

  • Ran go run generate.go in tsc/internal/bundled so embed_generated.go / libs_generated.go match the libs directory (only dropped a trailing blank line vs the cherry-pick auto-merge).

Feature kept

  • lib.es2025.json.d.ts with RawJSON, JSON.rawJSON, JSON.isRawJSON, and JSON.parse reviver context.source?
  • Reference from lib.es2025.d.ts, LibMap entry, compiler test + baselines

Note / remaining risk

main already ships the same APIs under lib.es2026.json.d.ts (via #64096), with a branded RawJSONInstance design and conformance tests under es2026/. This PR still places typings on es2025.json as originally proposed. Loading both (e.g. es2026 → references es2025) will merge the JSON/RawJSON declarations; worth a maintainer call on whether es2025.json is still desired given earlier feedback that the proposal is not in the ES2025 draft, and the existing es2026.json coverage.

Baselines for the new compiler test were carried from the prior PR tip (not re-run locally on Windows). CI should confirm.

@VedantMadane

Copy link
Copy Markdown
Author

Closing this PR as the feature has already landed upstream under lib.es2026.json.d.ts via #64096. Thank you!

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

Labels

For Backlog Bug PRs that fix a backlog bug Voight-Kampff Anomaly Repeated exogenous PRs look increasingly conceivable as non-human tinkering

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Support proposal-json-parse-with-source (JSON.rawJSON(), JSON.isRawJSON(), context parameter of reviver function, etc...)

9 participants