TypeScript: Type signature for JSON.stringify does not include undefined in the return type
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.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 46
- Comments: 16 (7 by maintainers)
Commits related to this issue
- lib.d.ts: Include undefined in return type of JSON.stringify() JSON.stringify() may return undefined if the given input value is undefined or if the input value is not serializable (like `function(){... — committed to MazeChaZer/TypeScript by deleted user 7 years ago
- lib.d.ts: Include undefined in return type of JSON.stringify() JSON.stringify() may return undefined if the given input value is undefined or if the input value is not serializable (like `function(){... — committed to MazeChaZer/TypeScript by deleted user 7 years ago
- lib.d.ts: Include undefined in return type of JSON.stringify() JSON.stringify() may return undefined if the given input value is undefined or if the input value is not serializable (like `function(){... — committed to MazeChaZer/TypeScript by deleted user 7 years ago
- Fix JSON.stringify return type (#18879), reduce usage of any in JSON.parse — committed to clarfonthey/TypeScript by clarfonthey 2 years ago
@sandersn can you reopen this bug since the PR got reverted ?
I just discovered a production bug that this would have caught. Any updates on it @MazeChaZer?
is there an open issue for this?
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
@mhegazy I suggest having both return value string|undefined and additional overload for undefined|Function|symbol to catch issues at compile time.