-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Conversation
@@ -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; |
There was a problem hiding this comment.
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;
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
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. |
JSON.stringify() may return undefined if the given input value is undefined or if the input value is not serializable (like
function(){}
).See #18879