-
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
Type signature for JSON.stringify does not include undefined in the return type #18879
Comments
This is also true for Edge:
https://docs.microsoft.com/en-us/scripting/javascript/reference/json-stringify-function-javascript |
So we need a new overload for |
Thanks for the quick response! That could work, please note that function showError(value: any): string {
const json = JSON.stringify(value);
// ...
// Later in the code, a string function was used
const lines = 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 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. |
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. |
👍 Will do |
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
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
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
I just discovered a production bug that this would have caught. Any updates on it @MazeChaZer? |
@mhegazy I suggest having both return value string|undefined and additional overload for undefined|Function|symbol to catch issues at compile time. |
Any progress on this one? (I got my fingers burned with this today) |
@RyanCavanaugh Can we triage this? There's no movement on the critical issue for 5 years. |
Here's a PR for this issue: #51897 |
Ready for review |
Any updates? |
@sandersn can you reopen this bug since the PR got reverted ? |
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: interface JSON { stringify(s: any): string | undefined; } 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 |
is there an open issue for this? |
|
Although calling
JSON.stringify
may return undefined (as documented here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#Description), it is not included in the type signature provided with TypeScript.The text was updated successfully, but these errors were encountered: