Skip to content
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

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

Closed
wants to merge 1 commit into from

Conversation

MazeChaZer
Copy link

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
Copy link

msftclas commented Oct 11, 2017

CLA assistant check
All CLA requirements met.

@@ -943,14 +943,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.

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
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants