Skip to content

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

Closed
Jonas Schürmann (MazeChaZer) wants to merge 1 commit into
microsoft:masterfrom
MazeChaZer:patch-2
Closed

Jonas Schürmann (MazeChaZer) wants to merge 1 commit into
microsoft:masterfrom
MazeChaZer:patch-2

Conversation

@MazeChaZer

Copy link
Copy Markdown

JSON.stringify() may return undefined if the given input value is undefined or if the input value is not serializable (like function(){}).
See #18879

@msftclas

Microsoft Contribution License Agreements (msftclas) commented Oct 11, 2017

Copy link
Copy Markdown

CLA assistant check
All CLA requirements met.

Comment thread src/lib/es5.d.ts
* @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
Copy Markdown
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
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@MazeChaZer Jonas Schürmann (MazeChaZer) Oct 11, 2017

Copy link
Copy Markdown
Author

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
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

sure.

JSON.stringify() may return undefined if the given input value is undefined or if the input value is not serializable (like `function(){}`).
See microsoft#18879
@typescript-bot

Copy link
Copy Markdown
Contributor

Thanks for your contribution. This PR has not been updated in a while and cannot be automatically merged at the time being. For housekeeping purposes we are closing stale PRs. If you'd still like to continue working on this PR, please leave a message and one of the maintainers can reopen it.

@microsoft Microsoft (microsoft) locked as resolved and limited conversation to collaborators Oct 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants