The use of the return type string
is clearly incorrect.
Introducing Typescript Generics can resolve this issue, which may not have been the case in 2017. Therefore, if you reach out to TS developers to inquire about updating this, here's how it can be accomplished now.
type JSONReturnType<T> =
T extends undefined ? undefined :
T extends bigint ? undefined :
T extends Symbol ? undefined :
string;
interface JSON {
/**
* Converts a JavaScript Object Notation (JSON) string into an object.
* @param text A valid JSON string.
* @param reviver A function that transforms the results. This function is called for each member of the object.
* If a member contains nested objects, the nested objects are transformed before the parent object is.
*/
parse(text: string, reviver?: (this: any, key: string, value: any) => any): any;
/**
* Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
* @param value A JavaScript value, usually an object or array, to be converted.
* @param replacer A function that transforms the results.
* @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
*/
stringify<T>(value: T, replacer?: (this: any, key: string, value: any) => any, space?: string | number): JSONReturnType<T>;
/**
* Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
* @param value A JavaScript value, usually an object or array, to be converted.
* @param replacer An array of strings and numbers that acts as an approved list for selecting the object properties that will be stringified.
* @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
*/
stringify<T>(value: T, replacer?: (number | string)[] | null, space?: string | number): JSONReturnType<T>;
}
/**
* An intrinsic object that provides functions to convert JavaScript values to and from the JavaScript Object Notation (JSON) format.
*/
declare var JSON: JSON;
const a = JSON.stringify({}); //=string
const b = JSON.stringify(undefined); //=undefined
const c = JSON.stringify(Symbol('')); //=undefined
const d = JSON.stringify(BigInt('10000')); //=undefined
TS Playground