You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Type signature for JSON.stringify does not include undefined in the return type #18879
That could work, please note that JSON.stringify(function() {}) also results in undefined.
Perhaps I should explain why this was a problem for me: I wrote an error handling function that would display JSON data
functionshowError(value: any): string{constjson=JSON.stringify(value);// ...// Later in the code, a string function was usedconstlines=json.split('/n');// This is where I got the runtime exception}
Would the overload catch this possible runtime error?
Something that would definitly work for me is a union return type that includes undefined, like so
stringify(value: any /* space, replacer, ... */): string |undefined
But of course I don't know conventions or guidelines for the type annotations very well, so this might be too broad.
Mohamed Hegazy (@mhegazy) I suggest having both return value string|undefined and additional overload for undefined|Function|symbol to catch issues at compile time.
Having tried this and looked into it more, I don't think we're going to try this again. People can add this line to their project to opt into this behavior if they want it:
Activity
MazeChaZer commentedon Oct 2, 2017
This is also true for Edge:
https://docs.microsoft.com/en-us/scripting/javascript/reference/json-stringify-function-javascript
mhegazy commentedon Oct 3, 2017
So we need a new overload for
stringify(value: undefined, .....): undefined;?MazeChaZer commentedon Oct 4, 2017
Thanks for the quick response!
That could work, please note that
JSON.stringify(function() {})also results inundefined.Perhaps I should explain why this was a problem for me: I wrote an error handling function that would display JSON data
Would the overload catch this possible runtime error?
Something that would definitly work for me is a union return type that includes
undefined, like soBut of course I don't know conventions or guidelines for the type annotations very well, so this might be too broad.
mhegazy commentedon Oct 4, 2017
PRs welcomed. You can find more information about contributing lib.d.ts fixes at https://github.com/Microsoft/TypeScript/blob/master/CONTRIBUTING.md#contributing-libdts-fixes.
MazeChaZer commentedon Oct 4, 2017
👍 Will do
lib.d.ts: Include undefined in return type of JSON.stringify()
lib.d.ts: Include undefined in return type of JSON.stringify()
lib.d.ts: Include undefined in return type of JSON.stringify()
bradleyayers commentedon Feb 6, 2018
I just discovered a production bug that this would have caught. Any updates on it Jonas Schürmann (@MazeChaZer)?
NN--- commentedon Apr 4, 2018
Mohamed Hegazy (@mhegazy) I suggest having both return value string|undefined and additional overload for undefined|Function|symbol to catch issues at compile time.
Fix JSON.stringify return type (microsoft#18879), reduce usage of any…
greyblake commentedon Nov 22, 2022
Any progress on this one? (I got my fingers burned with this today)
reverofevil commentedon Nov 26, 2022
Ryan Cavanaugh (@RyanCavanaugh) Can we triage this? There's no movement on the critical issue for 5 years.
ronyhe commentedon Dec 14, 2022
Here's a PR for this issue: #51897
This is my first attempt at contributing to the TS repo, advice and/or corrections would, of course, be welcome
ronyhe commentedon Dec 19, 2022
Ready for review
ronyhe commentedon Dec 21, 2022
Any updates?
Martin Johns (@MartinJohns) Is there anything I should be doing to advance this?
ecyrbe commentedon Jan 21, 2023
Nathan Shively-Sanders (@sandersn) can you reopen this bug since the PR got reverted ?
RyanCavanaugh commentedon Jan 23, 2023
Having tried this and looked into it more, I don't think we're going to try this again. People can add this line to their project to opt into this behavior if they want it:
but if we add this today, there is no real way to opt back in to today's behavior.
However, if at some point we make a "stricter" version of the core lib, this would definitely be a change to consider in that feature
DetachHead commentedon Mar 22, 2023
is there an open issue for this?
KotlinIsland commentedon Mar 22, 2023
silverwind commentedon Jul 31, 2025
I agree, this should be reopened. Just because it "breaks code" is not a reason to not fix it.
RyanCavanaugh commentedon Jul 31, 2025
https://github.com/Microsoft/TypeScript/wiki/FAQ#this-is-closed-but-should-be-open-or-vice-versa