Skip to content

lib.d.ts: Include undefined in return type of JSON.stringify() #19097

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/lib/es5.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -955,14 +955,14 @@ interface JSON {
* @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(value: any, replacer?: (key: string, value: any) => any, space?: string | number): string;
stringify(value: any, replacer?: (key: string, value: any) => any, space?: string | number): string | undefined;
Copy link
Contributor

Choose a reason for hiding this comment

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

I do not think you want everyone to have to handle undefined when calling JSON.stringify, it is just cases where hte input is undefined or possibly a function. for these, i would add overloads..e.g

stringify(value: number | string | boolean | null | object, replacer?: (key: string, value: any) => any, space?: string | number): string;
stringify(value: any, replacer?: (key: string, value: any) => any, space?: string | number): string | undefined;

Copy link
Contributor

Choose a reason for hiding this comment

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

also pinging @DanielRosenwasser and @RyanCavanaugh for feedback

Copy link
Author

@MazeChaZer MazeChaZer Oct 11, 2017

Choose a reason for hiding this comment

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

Yeah, narrowing down when undefined may appear would be great. I'm afraid this overload wouldn't catch function () {}, because it is of type object. Calling JSON.stringify(function () {}) would use the first provided overload, although the return value is undefined. Example on TypeScript Playground
Is it even possible to distinguish functions from “normal” objects in JavaScript? This is an area where I have very little knowledge.
Perhaps we can add one more overload before the other two that uses the Function type?

Copy link
Contributor

Choose a reason for hiding this comment

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

sure.

/**
* 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 a 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(value: any, replacer?: (number | string)[] | null, space?: string | number): string;
stringify(value: any, replacer?: (number | string)[] | null, space?: string | number): string | undefined;
}

/**
Expand Down
30 changes: 15 additions & 15 deletions tests/baselines/reference/json.stringify.types
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,50 @@ var value = null;
>null : null

JSON.stringify(value, undefined, 2);
>JSON.stringify(value, undefined, 2) : string
>JSON.stringify : { (value: any, replacer?: ((key: string, value: any) => any) | undefined, space?: string | number | undefined): string; (value: any, replacer?: (string | number)[] | null | undefined, space?: string | number | undefined): string; }
>JSON.stringify(value, undefined, 2) : string | undefined
>JSON.stringify : { (value: any, replacer?: ((key: string, value: any) => any) | undefined, space?: string | number | undefined): string | undefined; (value: any, replacer?: (string | number)[] | null | undefined, space?: string | number | undefined): string | undefined; }
>JSON : JSON
>stringify : { (value: any, replacer?: ((key: string, value: any) => any) | undefined, space?: string | number | undefined): string; (value: any, replacer?: (string | number)[] | null | undefined, space?: string | number | undefined): string; }
>stringify : { (value: any, replacer?: ((key: string, value: any) => any) | undefined, space?: string | number | undefined): string | undefined; (value: any, replacer?: (string | number)[] | null | undefined, space?: string | number | undefined): string | undefined; }
>value : null
>undefined : undefined
>2 : 2

JSON.stringify(value, null, 2);
>JSON.stringify(value, null, 2) : string
>JSON.stringify : { (value: any, replacer?: ((key: string, value: any) => any) | undefined, space?: string | number | undefined): string; (value: any, replacer?: (string | number)[] | null | undefined, space?: string | number | undefined): string; }
>JSON.stringify(value, null, 2) : string | undefined
>JSON.stringify : { (value: any, replacer?: ((key: string, value: any) => any) | undefined, space?: string | number | undefined): string | undefined; (value: any, replacer?: (string | number)[] | null | undefined, space?: string | number | undefined): string | undefined; }
>JSON : JSON
>stringify : { (value: any, replacer?: ((key: string, value: any) => any) | undefined, space?: string | number | undefined): string; (value: any, replacer?: (string | number)[] | null | undefined, space?: string | number | undefined): string; }
>stringify : { (value: any, replacer?: ((key: string, value: any) => any) | undefined, space?: string | number | undefined): string | undefined; (value: any, replacer?: (string | number)[] | null | undefined, space?: string | number | undefined): string | undefined; }
>value : null
>null : null
>2 : 2

JSON.stringify(value, ["a", 1], 2);
>JSON.stringify(value, ["a", 1], 2) : string
>JSON.stringify : { (value: any, replacer?: ((key: string, value: any) => any) | undefined, space?: string | number | undefined): string; (value: any, replacer?: (string | number)[] | null | undefined, space?: string | number | undefined): string; }
>JSON.stringify(value, ["a", 1], 2) : string | undefined
>JSON.stringify : { (value: any, replacer?: ((key: string, value: any) => any) | undefined, space?: string | number | undefined): string | undefined; (value: any, replacer?: (string | number)[] | null | undefined, space?: string | number | undefined): string | undefined; }
>JSON : JSON
>stringify : { (value: any, replacer?: ((key: string, value: any) => any) | undefined, space?: string | number | undefined): string; (value: any, replacer?: (string | number)[] | null | undefined, space?: string | number | undefined): string; }
>stringify : { (value: any, replacer?: ((key: string, value: any) => any) | undefined, space?: string | number | undefined): string | undefined; (value: any, replacer?: (string | number)[] | null | undefined, space?: string | number | undefined): string | undefined; }
>value : null
>["a", 1] : (string | number)[]
>"a" : "a"
>1 : 1
>2 : 2

JSON.stringify(value, (k) => undefined, 2);
>JSON.stringify(value, (k) => undefined, 2) : string
>JSON.stringify : { (value: any, replacer?: ((key: string, value: any) => any) | undefined, space?: string | number | undefined): string; (value: any, replacer?: (string | number)[] | null | undefined, space?: string | number | undefined): string; }
>JSON.stringify(value, (k) => undefined, 2) : string | undefined
>JSON.stringify : { (value: any, replacer?: ((key: string, value: any) => any) | undefined, space?: string | number | undefined): string | undefined; (value: any, replacer?: (string | number)[] | null | undefined, space?: string | number | undefined): string | undefined; }
>JSON : JSON
>stringify : { (value: any, replacer?: ((key: string, value: any) => any) | undefined, space?: string | number | undefined): string; (value: any, replacer?: (string | number)[] | null | undefined, space?: string | number | undefined): string; }
>stringify : { (value: any, replacer?: ((key: string, value: any) => any) | undefined, space?: string | number | undefined): string | undefined; (value: any, replacer?: (string | number)[] | null | undefined, space?: string | number | undefined): string | undefined; }
>value : null
>(k) => undefined : (k: string) => undefined
>k : string
>undefined : undefined
>2 : 2

JSON.stringify(value, undefined, 2);
>JSON.stringify(value, undefined, 2) : string
>JSON.stringify : { (value: any, replacer?: ((key: string, value: any) => any) | undefined, space?: string | number | undefined): string; (value: any, replacer?: (string | number)[] | null | undefined, space?: string | number | undefined): string; }
>JSON.stringify(value, undefined, 2) : string | undefined
>JSON.stringify : { (value: any, replacer?: ((key: string, value: any) => any) | undefined, space?: string | number | undefined): string | undefined; (value: any, replacer?: (string | number)[] | null | undefined, space?: string | number | undefined): string | undefined; }
>JSON : JSON
>stringify : { (value: any, replacer?: ((key: string, value: any) => any) | undefined, space?: string | number | undefined): string; (value: any, replacer?: (string | number)[] | null | undefined, space?: string | number | undefined): string; }
>stringify : { (value: any, replacer?: ((key: string, value: any) => any) | undefined, space?: string | number | undefined): string | undefined; (value: any, replacer?: (string | number)[] | null | undefined, space?: string | number | undefined): string | undefined; }
>value : null
>undefined : undefined
>2 : 2
Expand Down