Uh oh!
There was an error while loading. Please reload this page.
Add undefined to JSON.stringify return type - #51897
Conversation
reverofevil
commented
Dec 15, 2022
Even though this is kind of better than it was before, there's still a variety of cases when this typing fails. JSON.stringify({toJSON: ()=>undefined}) |
ronyhe
commented
Dec 15, 2022
Do you have a suggestion as to how to approach this? |
@microsoft-github-policy-service agree company="Wix.com" |
Once I wrote down "correct" types for JSON methods, and those were utterly dreadful. Correct approach is to reexport them somewhere in your project with types tightened as much as possible (without any undefined, toJSON, replacers and overloads allowed), and avoid spending any time to handle accidental complexity of EcmaScript standard and TS type system. It doesn't pay off. |
ronyhe
commented
Dec 15, 2022
Then let's leave the |
ronyhe
commented
Dec 15, 2022
I'm considering extracting out the replacer type: typeJsonReplacer=number[]|string[]|((this: any,key: string,value: any)=>any)interfaceJSON{/** * Converts a JavaScript Object Notation (JSON) string into an object. * @param text A valid JSON string. * @param reviver A function that transforms the results. This function is called for each member of the object. * If a member contains nested objects, the nested objects are transformed before the parent object is. */parse(text: string,reviver?: (this: any,key: string,value: any)=>any): any;/** * Converts a JavaScript value to a JavaScript Object Notation (JSON) string. * @param value A JavaScript value, usually an object or array, to be converted. * @param replacer An array of strings and numbers that acts as an approved list for selecting the object properties that will be stringified. * @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read. */stringify(value: Function|Symbol|undefined,replacer?: JsonReplacer|null,space?: string|number): undefined;/** * Converts a JavaScript value to a JavaScript Object Notation (JSON) string. * @param value A JavaScript value, usually an object or array, to be converted. * @param replacer A function that transforms the results. * @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read. */stringify(value: any,replacer?: JsonReplacer|null,space?: string|number): string;}Any thoughts? |
Martin Johns (MartinJohns)
commented
Dec 15, 2022
ronyhe What for? It's only used twice, and moving it to a dedicated type is yet another potential break (if someone has such a type defined). Also, the TypeScript team generally doesn't want to add more utility types:
|
Martin Johns (@MartinJohns) The original replacer type has two options (not including
|
Martin Johns (MartinJohns)
commented
Dec 16, 2022
I don't understand this. Extracting it to a utility type it's still a union. And overloads are not interchangeably with unions, they behave very much different. |
ronyhe
commented
Dec 16, 2022
Martin Johns (@MartinJohns) Yes, I suppose you're right. |
This looks reasonable to me and I can't think of ways for it to break existing code. I'm going to run some user tests on it to double-check. TypeScript Bot (@typescript-bot) user test this |
Heya Nathan Shively-Sanders (@sandersn), I've started to run the diff-based top-repos suite on this PR at 2da11c0. You can monitor the build here. Update: The results are in! |
Heya Nathan Shively-Sanders (@sandersn), I've started to run the diff-based user code test suite on this PR at 2da11c0. You can monitor the build here. Update: The results are in! |
Heya Nathan Shively-Sanders (@sandersn), I've started to run the parallelized Definitely Typed test suite on this PR at 2da11c0. You can monitor the build here. |
TypeScript Bot (typescript-bot)
commented
Dec 27, 2022
Nathan Shively-Sanders (@sandersn) Here are the results of running the user test suite comparing Everything looks good! |
TypeScript Bot (typescript-bot)
commented
Dec 27, 2022
Nathan Shively-Sanders (@sandersn) Here are the results of running the top-repos suite comparing Everything looks good! |
reverofevil
commented
Dec 28, 2022
That was totally unexpected. Great job, ronyhe! |
ronyhe
commented
Dec 28, 2022
Thanks @polkovnikov-ph and thank you Martin Johns (@MartinJohns) and Nathan Shively-Sanders (@sandersn) for the help! |
This reverts commit c7f49bc.
Add undefined to JSON.stringify return type when input value is Function | Symbol | undefined
Fixes#18879