Skip to content

Commit f26c011

Browse files
committed
Fix return type for JSON.stringify
1 parent 67d7842 commit f26c011

19 files changed

+281
-67
lines changed

src/lib/es5.d.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,17 +1040,40 @@ interface JSON {
10401040
/**
10411041
* Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
10421042
* @param value A JavaScript value, usually an object or array, to be converted.
1043-
* @param replacer A function that transforms the results.
1043+
* @param replacer An array of strings and numbers that acts as a approved list for selecting the object properties that will be stringified.
10441044
* @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
10451045
*/
1046-
stringify(value: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number): string;
1046+
stringify(
1047+
value: number | string | boolean | null | unknown[],
1048+
replacer?: (number | string)[] | null,
1049+
space?: string | number
1050+
): string;
1051+
stringify(
1052+
value: undefined | Function | Symbol,
1053+
replacer?: (number | string)[] | null,
1054+
space?: any
1055+
): undefined;
1056+
stringify(
1057+
value: object,
1058+
replacer?: (number | string)[] | null,
1059+
space?: string | number
1060+
): string;
1061+
stringify(
1062+
value: any,
1063+
replacer?: (number | string)[] | null,
1064+
space?: string | number
1065+
): string | undefined;
10471066
/**
10481067
* Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
10491068
* @param value A JavaScript value, usually an object or array, to be converted.
1050-
* @param replacer An array of strings and numbers that acts as a approved list for selecting the object properties that will be stringified.
1069+
* @param replacer A function that transforms the results.
10511070
* @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
10521071
*/
1053-
stringify(value: any, replacer?: (number | string)[] | null, space?: string | number): string;
1072+
stringify(
1073+
value: any,
1074+
replacer?: (this: any, key: string, value: any) => any,
1075+
space?: string | number
1076+
): string | undefined;
10541077
}
10551078

10561079
/**

tests/baselines/reference/controlFlowPropertyDeclarations.symbols

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,9 @@ export class HTMLtoJSX {
232232
>whitespace : Symbol(whitespace, Decl(controlFlowPropertyDeclarations.ts, 121, 50))
233233

234234
return '{' + JSON.stringify(whitespace) + '}';
235-
>JSON.stringify : Symbol(JSON.stringify, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
235+
>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, --, --))
236236
>JSON : Symbol(JSON, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
237-
>stringify : Symbol(JSON.stringify, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
237+
>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, --, --))
238238
>whitespace : Symbol(whitespace, Decl(controlFlowPropertyDeclarations.ts, 121, 50))
239239

240240
});

tests/baselines/reference/controlFlowPropertyDeclarations.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,9 +316,9 @@ export class HTMLtoJSX {
316316
>'{' + JSON.stringify(whitespace) : string
317317
>'{' : "{"
318318
>JSON.stringify(whitespace) : string
319-
>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; }
319+
>JSON.stringify : { (value: string | number | boolean | unknown[], replacer?: (string | number)[], space?: string | number): string; (value: Function | Symbol, replacer?: (string | number)[], space?: any): undefined; (value: object, replacer?: (string | number)[], space?: string | number): string; (value: any, replacer?: (string | number)[], space?: string | number): string; (value: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number): string; }
320320
>JSON : JSON
321-
>stringify : { (value: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number): string; (value: any, replacer?: (string | number)[], space?: string | number): string; }
321+
>stringify : { (value: string | number | boolean | unknown[], replacer?: (string | number)[], space?: string | number): string; (value: Function | Symbol, replacer?: (string | number)[], space?: any): undefined; (value: object, replacer?: (string | number)[], space?: string | number): string; (value: any, replacer?: (string | number)[], space?: string | number): string; (value: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number): string; }
322322
>whitespace : string
323323
>'}' : "}"
324324

tests/baselines/reference/destructuringInitializerContextualTypeFromContext.symbols

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ const Child: SFC<Props> = ({
5151

5252
}) => `name: ${name} props: ${JSON.stringify(props)}`;
5353
>name : Symbol(name, Decl(destructuringInitializerContextualTypeFromContext.ts, 15, 13))
54-
>JSON.stringify : Symbol(JSON.stringify, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
54+
>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, --, --))
5555
>JSON : Symbol(JSON, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
56-
>stringify : Symbol(JSON.stringify, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
56+
>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, --, --))
5757
>props : Symbol(props, Decl(destructuringInitializerContextualTypeFromContext.ts, 16, 21))
5858

5959
// Repro from #29189

tests/baselines/reference/destructuringInitializerContextualTypeFromContext.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ const Child: SFC<Props> = ({
5050
>`name: ${name} props: ${JSON.stringify(props)}` : string
5151
>name : "Apollo" | "Artemis" | "Dionysus" | "Persephone"
5252
>JSON.stringify(props) : string
53-
>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; }
53+
>JSON.stringify : { (value: string | number | boolean | unknown[] | null, replacer?: (string | number)[] | null | undefined, space?: string | number | undefined): string; (value: Function | Symbol | undefined, replacer?: (string | number)[] | null | undefined, space?: any): undefined; (value: object, replacer?: (string | number)[] | null | undefined, space?: string | number | undefined): string; (value: any, replacer?: (string | number)[] | null | undefined, space?: string | number | undefined): string | undefined; (value: any, replacer?: ((this: any, key: string, value: any) => any) | undefined, space?: string | number | undefined): string | undefined; }
5454
>JSON : JSON
55-
>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; }
55+
>stringify : { (value: string | number | boolean | unknown[] | null, replacer?: (string | number)[] | null | undefined, space?: string | number | undefined): string; (value: Function | Symbol | undefined, replacer?: (string | number)[] | null | undefined, space?: any): undefined; (value: object, replacer?: (string | number)[] | null | undefined, space?: string | number | undefined): string; (value: any, replacer?: (string | number)[] | null | undefined, space?: string | number | undefined): string | undefined; (value: any, replacer?: ((this: any, key: string, value: any) => any) | undefined, space?: string | number | undefined): string | undefined; }
5656
>props : {}
5757

5858
// Repro from #29189

tests/baselines/reference/destructuringParameterDeclaration4.errors.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts(
4141
a1(...array2); // Error parameter type is (number|string)[]
4242
~~~~~~
4343
!!! error TS2552: Cannot find name 'array2'. Did you mean 'Array'?
44-
!!! related TS2728 /.ts/lib.es5.d.ts:1385:13: 'Array' is declared here.
44+
!!! related TS2728 /.ts/lib.es5.d.ts:1408:13: 'Array' is declared here.
4545
a5([1, 2, "string", false, true]); // Error, parameter type is [any, any, [[any]]]
4646
~~~~~~~~
4747
!!! error TS2322: Type 'string' is not assignable to type '[[any]]'.

tests/baselines/reference/destructuringTuple.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ tests/cases/compiler/destructuringTuple.ts(11,60): error TS2769: No overload mat
3535
!!! error TS2769: Type 'never[]' is not assignable to type '[]'.
3636
!!! error TS2769: Types of property 'length' are incompatible.
3737
!!! error TS2769: Type 'number' is not assignable to type '0'.
38-
!!! related TS6502 /.ts/lib.es5.d.ts:1350:24: The expected type comes from the return type of this signature.
39-
!!! related TS6502 /.ts/lib.es5.d.ts:1356:27: The expected type comes from the return type of this signature.
38+
!!! related TS6502 /.ts/lib.es5.d.ts:1373:24: The expected type comes from the return type of this signature.
39+
!!! related TS6502 /.ts/lib.es5.d.ts:1379:27: The expected type comes from the return type of this signature.
4040
~~
4141
!!! error TS2769: No overload matches this call.
4242
!!! error TS2769: Overload 1 of 2, '(...items: ConcatArray<never>[]): never[]', gave the following error.
Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,40 @@
11
//// [json.stringify.ts]
2+
var replacer = (k) => undefined;
23
var value = null;
34
JSON.stringify(value, undefined, 2);
45
JSON.stringify(value, null, 2);
56
JSON.stringify(value, ["a", 1], 2);
6-
JSON.stringify(value, (k) => undefined, 2);
7-
JSON.stringify(value, undefined, 2);
7+
JSON.stringify(value, replacer, 2);
8+
JSON.stringify(value, undefined, 2);
9+
JSON.stringify(undefined);
10+
JSON.stringify(undefined, replacer);
11+
JSON.stringify(() => '');
12+
JSON.stringify(() => '', replacer);
13+
JSON.stringify({});
14+
JSON.stringify({}, replacer);
15+
JSON.stringify(new Object());
16+
JSON.stringify(new Object(), replacer);
17+
var anyValue: any;
18+
JSON.stringify(anyValue);
19+
JSON.stringify(anyValue, replacer);
20+
821

922
//// [json.stringify.js]
23+
var replacer = function (k) { return undefined; };
1024
var value = null;
1125
JSON.stringify(value, undefined, 2);
1226
JSON.stringify(value, null, 2);
1327
JSON.stringify(value, ["a", 1], 2);
14-
JSON.stringify(value, function (k) { return undefined; }, 2);
28+
JSON.stringify(value, replacer, 2);
1529
JSON.stringify(value, undefined, 2);
30+
JSON.stringify(undefined);
31+
JSON.stringify(undefined, replacer);
32+
JSON.stringify(function () { return ''; });
33+
JSON.stringify(function () { return ''; }, replacer);
34+
JSON.stringify({});
35+
JSON.stringify({}, replacer);
36+
JSON.stringify(new Object());
37+
JSON.stringify(new Object(), replacer);
38+
var anyValue;
39+
JSON.stringify(anyValue);
40+
JSON.stringify(anyValue, replacer);

0 commit comments

Comments
 (0)