Skip to content
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

Fix return type for JSON.stringify(any) -> string #38574

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions src/lib/es5.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1040,17 +1040,40 @@ interface JSON {
/**
* 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 replacer An array of strings and numbers that acts as a 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: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number): string;
stringify(
value: number | string | boolean | null | object,
replacer?: (number | string)[] | null,
space?: string | number
): string;
stringify(
value: undefined | Symbol,
replacer?: (number | string)[] | null,
space?: any

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why space is any here?

): undefined;
stringify(
jupiter marked this conversation as resolved.
Show resolved Hide resolved
value: any,
replacer?: (number | string)[] | null,
space?: string | number
): 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: any, replacer?: (number | string)[] | null, space?: string | number): string;
stringify(
value: any,
replacer: (this: any, key: string, value: any) => number | string | boolean | null | object,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JSON.stringify({}, () => () => {})

uses this overload and returns string instead of undefined.

space?: string | number
): string;
stringify(
value: any,
replacer?: (this: any, key: string, value: any) => any,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think replacer shouldn't be optional here.

space?: string | number
): any;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,9 @@ export class HTMLtoJSX {
>whitespace : Symbol(whitespace, Decl(controlFlowPropertyDeclarations.ts, 121, 50))

return '{' + JSON.stringify(whitespace) + '}';
>JSON.stringify : Symbol(JSON.stringify, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>JSON.stringify : Symbol(JSON.stringify, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>JSON : Symbol(JSON, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>stringify : Symbol(JSON.stringify, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>stringify : Symbol(JSON.stringify, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>whitespace : Symbol(whitespace, Decl(controlFlowPropertyDeclarations.ts, 121, 50))

});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,9 @@ export class HTMLtoJSX {
>'{' + JSON.stringify(whitespace) : string
>'{' : "{"
>JSON.stringify(whitespace) : string
>JSON.stringify : { (value: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number): string; (value: any, replacer?: (string | number)[], space?: string | number): string; }
>JSON.stringify : { (value: string | number | boolean | object, replacer?: (string | number)[], space?: string | number): string; (value: Symbol, replacer?: (string | number)[], space?: any): undefined; (value: any, replacer?: (string | number)[], space?: string | number): any; (value: any, replacer: (this: any, key: string, value: any) => string | number | boolean | object, space?: string | number): string; (value: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number): any; }
>JSON : JSON
>stringify : { (value: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number): string; (value: any, replacer?: (string | number)[], space?: string | number): string; }
>stringify : { (value: string | number | boolean | object, replacer?: (string | number)[], space?: string | number): string; (value: Symbol, replacer?: (string | number)[], space?: any): undefined; (value: any, replacer?: (string | number)[], space?: string | number): any; (value: any, replacer: (this: any, key: string, value: any) => string | number | boolean | object, space?: string | number): string; (value: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number): any; }
>whitespace : string
>'}' : "}"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ const Child: SFC<Props> = ({

}) => `name: ${name} props: ${JSON.stringify(props)}`;
>name : Symbol(name, Decl(destructuringInitializerContextualTypeFromContext.ts, 15, 13))
>JSON.stringify : Symbol(JSON.stringify, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>JSON.stringify : Symbol(JSON.stringify, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>JSON : Symbol(JSON, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>stringify : Symbol(JSON.stringify, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>stringify : Symbol(JSON.stringify, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>props : Symbol(props, Decl(destructuringInitializerContextualTypeFromContext.ts, 16, 21))

// Repro from #29189
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ const Child: SFC<Props> = ({
>`name: ${name} props: ${JSON.stringify(props)}` : string
>name : "Apollo" | "Artemis" | "Dionysus" | "Persephone"
>JSON.stringify(props) : string
>JSON.stringify : { (value: any, replacer?: ((this: any, key: string, value: any) => any) | undefined, space?: string | number | undefined): string; (value: any, replacer?: (string | number)[] | null | undefined, space?: string | number | undefined): string; }
>JSON.stringify : { (value: string | number | boolean | object | null, replacer?: (string | number)[] | null | undefined, space?: string | number | undefined): string; (value: Symbol | undefined, replacer?: (string | number)[] | null | undefined, space?: any): undefined; (value: any, replacer?: (string | number)[] | null | undefined, space?: string | number | undefined): any; (value: any, replacer: (this: any, key: string, value: any) => string | number | boolean | object | null, space?: string | number | undefined): string; (value: any, replacer?: ((this: any, key: string, value: any) => any) | undefined, space?: string | number | undefined): any; }
>JSON : JSON
>stringify : { (value: any, replacer?: ((this: any, key: string, value: any) => any) | undefined, space?: string | number | undefined): string; (value: any, replacer?: (string | number)[] | null | undefined, space?: string | number | undefined): string; }
>stringify : { (value: string | number | boolean | object | null, replacer?: (string | number)[] | null | undefined, space?: string | number | undefined): string; (value: Symbol | undefined, replacer?: (string | number)[] | null | undefined, space?: any): undefined; (value: any, replacer?: (string | number)[] | null | undefined, space?: string | number | undefined): any; (value: any, replacer: (this: any, key: string, value: any) => string | number | boolean | object | null, space?: string | number | undefined): string; (value: any, replacer?: ((this: any, key: string, value: any) => any) | undefined, space?: string | number | undefined): any; }
>props : {}

// Repro from #29189
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts(
a1(...array2); // Error parameter type is (number|string)[]
~~~~~~
!!! error TS2552: Cannot find name 'array2'. Did you mean 'Array'?
!!! related TS2728 /.ts/lib.es5.d.ts:1415:13: 'Array' is declared here.
!!! related TS2728 /.ts/lib.es5.d.ts:1438:13: 'Array' is declared here.
a5([1, 2, "string", false, true]); // Error, parameter type is [any, any, [[any]]]
~~~~~~~~
!!! error TS2322: Type 'string' is not assignable to type '[[any]]'.
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/destructuringTuple.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ tests/cases/compiler/destructuringTuple.ts(11,60): error TS2769: No overload mat
!!! error TS2769: Overload 2 of 3, '(callbackfn: (previousValue: [], currentValue: number, currentIndex: number, array: number[]) => [], initialValue: []): []', gave the following error.
!!! error TS2769: Type 'never[]' is not assignable to type '[]'.
!!! error TS2769: Target allows only 0 element(s) but source may have more.
!!! related TS6502 /.ts/lib.es5.d.ts:1380:24: The expected type comes from the return type of this signature.
!!! related TS6502 /.ts/lib.es5.d.ts:1386:27: The expected type comes from the return type of this signature.
!!! related TS6502 /.ts/lib.es5.d.ts:1403:24: The expected type comes from the return type of this signature.
!!! related TS6502 /.ts/lib.es5.d.ts:1409:27: The expected type comes from the return type of this signature.
~~
!!! error TS2769: No overload matches this call.
!!! error TS2769: Overload 1 of 2, '(...items: ConcatArray<never>[]): never[]', gave the following error.
Expand Down
43 changes: 40 additions & 3 deletions tests/baselines/reference/json.stringify.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,52 @@
//// [json.stringify.ts]
var anyReplacer = (k) => undefined;
var failsafeReplacer = (k) => null;
var value = null;
JSON.stringify(value, undefined, 2);
JSON.stringify(value, null, 2);
JSON.stringify(value, ["a", 1], 2);
JSON.stringify(value, (k) => undefined, 2);
JSON.stringify(value, undefined, 2);
JSON.stringify(value, anyReplacer, 2);
JSON.stringify(value, failsafeReplacer, 2);
JSON.stringify(value, undefined, 2);
JSON.stringify(undefined);
JSON.stringify(undefined, anyReplacer);
JSON.stringify(undefined, failsafeReplacer);
JSON.stringify(() => "", anyReplacer);
JSON.stringify(() => "", failsafeReplacer);
JSON.stringify({});
JSON.stringify({}, anyReplacer);
JSON.stringify({}, failsafeReplacer);
JSON.stringify(new Object());
JSON.stringify(new Object(), anyReplacer);
JSON.stringify(new Object(), failsafeReplacer);
var anyValue: any;
JSON.stringify(anyValue);
JSON.stringify(anyValue, anyReplacer);
JSON.stringify(anyValue, failsafeReplacer);


//// [json.stringify.js]
var anyReplacer = function (k) { return undefined; };
var failsafeReplacer = function (k) { return null; };
var value = null;
JSON.stringify(value, undefined, 2);
JSON.stringify(value, null, 2);
JSON.stringify(value, ["a", 1], 2);
JSON.stringify(value, function (k) { return undefined; }, 2);
JSON.stringify(value, anyReplacer, 2);
JSON.stringify(value, failsafeReplacer, 2);
JSON.stringify(value, undefined, 2);
JSON.stringify(undefined);
JSON.stringify(undefined, anyReplacer);
JSON.stringify(undefined, failsafeReplacer);
JSON.stringify(function () { return ""; }, anyReplacer);
JSON.stringify(function () { return ""; }, failsafeReplacer);
JSON.stringify({});
JSON.stringify({}, anyReplacer);
JSON.stringify({}, failsafeReplacer);
JSON.stringify(new Object());
JSON.stringify(new Object(), anyReplacer);
JSON.stringify(new Object(), failsafeReplacer);
var anyValue;
JSON.stringify(anyValue);
JSON.stringify(anyValue, anyReplacer);
JSON.stringify(anyValue, failsafeReplacer);
Loading