The type definition for JSON.stringify erroneously has return type string
#34630
Labels
Duplicate
An existing issue was already created
string
#34630
JSON.stringify
currently has type:For all inputs (including
null
) exceptundefined
, this type is correct. However,JSON.stringify(undefined)
returnsundefined
. Therefore, the return type ofJSON.stringify
should be changed tostring | undefined
.Solution 1: Change return type for existing overloads
Solution 2 (untested): Only change the return type for undefined inputs
TypeScript Version:
3.6.3
and3.7-Beta
(on typescript playground),3.6.4
(locally)Search Terms:
stringify
JSON.stringify
Code
Expected behavior:
Code that assumes that the result of
JSON.stringify
is notundefined
should fail to compile.Actual behavior:
Code that assumes that the result of
JSON.stringify
is notundefined
compiles, but fails at runtime.Playground Link:
http://www.typescriptlang.org/play/?ssl=3&ssc=93&pln=3&pc=61#code/MYewdgzgLgBATgUwgVwDawLwwFIGUDyAcgHTRwCWYA5uQGYCeAFMmACYK2UKsCUAUKEghUCYqhBVGiFOh4wA9PJgt2nMNwHgIw0eMnS0UMQmpQAFnMUwAqmGABDZFTOwAKvQAOCAKJw4IOAAuGABhezAwEFhEe1YYD38vOCh6GAByEVMzNJgQWmU2Di5WIA
Related Issues:
The text was updated successfully, but these errors were encountered: