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

Most upvoted comments

@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?

if at some point we make a “stricter” version of the core lib,

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:

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

@mhegazy I suggest having both return value string|undefined and additional overload for undefined|Function|symbol to catch issues at compile time.