Skip to content

JSON.stringify can return undefined #29962

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

WearyMonkey
Copy link

According to Note 2 of the JSON.stringify spec:

The undefined value is not rendered.

https://www.ecma-international.org/ecma-262/6.0/#sec-json.stringify

After this change, the return type of JSON.stringify will be string | undefined if the value parameter extends undefined.

Fixes #18879

@msftclas
Copy link

msftclas commented Feb 19, 2019

CLA assistant check
All CLA requirements met.

@cevek
Copy link

cevek commented Mar 12, 2019

I may be undefined only if ReplacerFunction is specified

@RyanCavanaugh
Copy link
Member

I think it'd be preferable to write this as overloads

stringify(x: object | number | boolean | string | null): string;
stringify(x: unknown): string | undefined;

This way invocations of stringify(any) maintain their current behavior of returning string (this is going to be far too large of a breaking change otherwise), but identifiably-unsafe calls like stringify(object | undefined) correctly include undefined in the return type.

@RyanCavanaugh
Copy link
Member

Closing due to continued CI failure

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Type signature for JSON.stringify does not include undefined in the return type
4 participants