Skip to content

fix(types): make parseReviver context.source optional per ES2023 spec#10837

Merged
jasonsaayman merged 2 commits into
axios:v1.xfrom
mixelburg:fix/parseReviver-context-source-optional
May 6, 2026
Merged

fix(types): make parseReviver context.source optional per ES2023 spec#10837
jasonsaayman merged 2 commits into
axios:v1.xfrom
mixelburg:fix/parseReviver-context-source-optional

Conversation

@mixelburg
Copy link
Copy Markdown
Contributor

@mixelburg mixelburg commented May 2, 2026

Background

PR #10782 added the ES2023 context argument to the parseReviver type:

parseReviver?: (this: any, key: string, value: any, context?: { source: string }) => any;

This is correct as far as it goes, but is slightly looser than the spec.

The Gap

Per the TC39 proposal-json-parse-with-source, context.source is only set when the reviver is invoked on a primitive value (string, number, boolean, null). When the reviver is invoked on an object or array, source is absent from the context object.

The current typing says "if context is provided, source is always a string." That's true for primitives but not for object/array reviver invocations. A user can legally write:

parseReviver: (key, value, context) => {
  if (typeof value === 'object') {
    return context.source.length; // types say string, runtime undefined -> crash
  }
}

Fix

Mark source as optional:

parseReviver?: (this: any, key: string, value: any, context?: { source?: string }) => any;

Applied to both index.d.ts and index.d.cts.

Fixes #10817


Summary by cubic

Make parseReviver context source optional in AxiosRequestConfig to match ES2023 JSON.parse with source. Corrects typing that implied source was always present; no runtime changes.

Description

  • Summary of changes
    • Updated parseReviver type to context?: { source?: string } in index.d.ts and index.d.cts.
    • Merged latest v1.x with no additional functional changes.
  • Reasoning
    • ES2023 sets context.source only for primitive values; it’s absent for objects/arrays.
  • Additional context
    • TypeScript users should guard context?.source before use.

Docs

  • Update /docs/ to note context.source is optional and present only for primitives, with a short guarded example.

Testing

  • No runtime tests changed.
  • Recommend a small type test to ensure context.source is optional and narrows with guards.

Semantic version impact

Patch: typings corrected to match the spec; no runtime API changes.

Written for commit f9372e5. Summary will update on new commits.

Per the TC39 proposal-json-parse-with-source, context.source is only
set when the reviver is invoked on a primitive value (string, number,
boolean, null). For object/array values, source is absent from the
context object.

Changed context?: { source: string } to context?: { source?: string }
in both index.d.ts and index.d.cts.

Fixes axios#10817
@mixelburg mixelburg requested a review from jasonsaayman as a code owner May 2, 2026 22:22
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

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

No issues found across 2 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

@jasonsaayman jasonsaayman added priority::medium A medium priority commit::fix The PR is related to a bugfix labels May 6, 2026
@jasonsaayman jasonsaayman merged commit ac5c335 into axios:v1.x May 6, 2026
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

commit::fix The PR is related to a bugfix priority::medium A medium priority

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tighten parseReviver context type to reflect ES2023 spec (source is only present for primitives)

2 participants