lib.d.ts: Include undefined in return type of JSON.stringify() - #19097
Jonas Schürmann (MazeChaZer) wants to merge 1 commit into
Conversation
04be41c to
90cbb5f
Compare
| * @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.
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.
also pinging Daniel Rosenwasser (@DanielRosenwasser) and Ryan Cavanaugh (@RyanCavanaugh) for feedback
There was a problem hiding this comment.
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?
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
90cbb5f to
96a3fd6
Compare
|
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