Uh oh!
There was an error while loading. Please reload this page.
Fix return type for JSON.stringify(any) -> string - #38574
Fix return type for JSON.stringify(any) -> string#38574Pieter Raubenheimer (jupiter) wants to merge 9 commits into
Conversation
TypeScript Bot (typescript-bot)
commented
May 14, 2020
It looks like you've sent a pull request to update our 'lib' files. These files aren't meant to be edited by hand, as they consist of last-known good states of the compiler and are generated from 'src'. Unless this is necessary, consider closing the pull request and sending a separate PR to update 'src'. |
Uh oh!
There was an error while loading. Please reload this page.
Ryan Cavanaugh (RyanCavanaugh)
commented
May 14, 2020
TypeScript Bot (@typescript-bot) test this |
Heya Ryan Cavanaugh (@RyanCavanaugh), I've started to run the parallelized community code test suite on this PR at f26c011. You can monitor the build here. |
Heya Ryan Cavanaugh (@RyanCavanaugh), I've started to run the extended test suite on this PR at f26c011. You can monitor the build here. |
TypeScript Bot (typescript-bot)
commented
May 14, 2020
The user suite test run you requested has finished and failed. I've opened a PR with the baseline diff from master. |
True, perhaps there’s a compromise to be made. I’ll have a go at removing the `any` input overload, or at least make that also return `any`, so that it is at least not a falsehood. |
Pieter Raubenheimer (jupiter)
commented
May 22, 2020
Ryan Cavanaugh (@RyanCavanaugh) Can we see how the update does with user tests? I'm thinking that |
WJH (wongjiahau)
commented
Jun 13, 2020
Hopefully this can be merged soon, because this issue is bugging me a lot. |
Igor Adamenko (igoradamenko)
commented
Sep 29, 2020
Same here. I've shot myself in the foot because of |
Nate Higgins (nathggns)
commented
Feb 16, 2021
Is this not progressing due to conflicts/an issue with the fix, or has there just not been time to merge it yet? I.e, is the blocker with the author or with maintainers? Trying to evaluate whether to submit a PR myself or not as was just bitten by this bug. (Not trying to spam here – just wondering if I can assist) |
Pieter Raubenheimer (@jupiter) Are you sure these types are correct? I think it doesn't handle at least Update. Found that |
| stringify(value: any, replacer?: (number | string)[] | null, space?: string | number): string; | ||
| stringify( | ||
| value: any, | ||
| replacer: (this: any, key: string, value: any) => number | string | boolean | null | object, |
There was a problem hiding this comment.
JSON.stringify({}, () => () => {})
uses this overload and returns string instead of undefined.
| ): string; | ||
| stringify( | ||
| value: any, | ||
| replacer?: (this: any, key: string, value: any) => any, |
There was a problem hiding this comment.
I think replacer shouldn't be optional here.
| stringify( | ||
| value: undefined | Symbol, | ||
| replacer?: (number | string)[] | null, | ||
| space?: any |
reverofevil
commented
Mar 7, 2021
Funny enough, even the best types I could imagine are not enough. Due to some |
Mahmoud Soltan (mtsoltan)
commented
Feb 10, 2022
Can this, or some other PR that fixes #18879 , get looked at again? |
Unfortunately, we never finished reviewing this PR. It is pretty old now, so I'm going to close it to reduce the number of open PRs. |
Fixes#18879
Problem
Current type definitions falsely assert that
JSON.stringify(...)always returns a string.JSON.stringify(...)actually returnsundefinedwhenever the input value isundefined, a Function, or a Symbol (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify). It also returnsundefinedwhen a provided replacer function returns any of these types for the root value.The fix
We provide overloads to ensure that when valid JSON values are provided, the return type is
string. When an invalid value is provided, the return type isundefinedas per runtime behaviour. When the input value type isanyor when a replacer function is passed the return type isstring | undefined.