Background
PR #10782 added the ES2023 context argument to the parseReviver type:
// index.d.ts:441 / index.d.cts:544
parseReviver?: (this: any, key: string, value: any, context?: { source: string }) => any;
This is correct as far as it goes, but it's 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
}
}
Proposed fix
Mark source as optional:
parseReviver?: (this: any, key: string, value: any, context?: { source?: string }) => any;
Apply to both index.d.ts and index.d.cts.
Acceptance criteria
Context
Patch-level change; types only.
Background
PR #10782 added the ES2023
contextargument to theparseRevivertype:This is correct as far as it goes, but it's slightly looser than the spec.
The gap
Per the TC39 proposal-json-parse-with-source,
context.sourceis 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,sourceis absent from the context object.The current typing says "if
contextis provided,sourceis always a string." That's true for primitives but not for object/array reviver invocations. A user can legally write:Proposed fix
Mark
sourceas optional:Apply to both
index.d.tsandindex.d.cts.Acceptance criteria
parseReviversignature inindex.d.tsandindex.d.ctstocontext?: { source?: string }tsd(or equivalent dtslint) test asserting:context.sourceis typed asstring | undefined, notstringdocs/pages/advanced/request-config.mdif it relies on the looser typing (the existing examples already usecontext?.sourcewhich works either way)Context
parseReviverTypeScript definitions for ES2023 and add to official configuration documentation #10782index.d.ts,index.d.ctslib/defaults/index.js:129forwards directly to nativeJSON.parsePatch-level change; types only.