Skip to content

Commit

Permalink
Fix return type for JSON.stringify
Browse files Browse the repository at this point in the history
  • Loading branch information
jupiter committed May 14, 2020
1 parent 67d7842 commit f26c011
Show file tree
Hide file tree
Showing 19 changed files with 281 additions and 67 deletions.
31 changes: 27 additions & 4 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 | unknown[],
replacer?: (number | string)[] | null,
space?: string | number
): string;
stringify(
value: undefined | Function | Symbol,
replacer?: (number | string)[] | null,
space?: any
): undefined;
stringify(
value: object,
replacer?: (number | string)[] | null,
space?: string | number
): string;
stringify(
value: any,
replacer?: (number | string)[] | null,
space?: string | number
): string | 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 An array of strings and numbers that acts as a approved list for selecting the object properties that will be stringified.
* @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?: (number | string)[] | null, space?: string | number): string;
stringify(
value: any,
replacer?: (this: any, key: string, value: any) => any,
space?: string | number
): string | undefined;
}

/**
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 | 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; }
>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 | 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; }
>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 | 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; }
>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 | 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; }
>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:1385:13: 'Array' is declared here.
!!! related TS2728 /.ts/lib.es5.d.ts:1408: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 @@ -35,8 +35,8 @@ tests/cases/compiler/destructuringTuple.ts(11,60): error TS2769: No overload mat
!!! error TS2769: Type 'never[]' is not assignable to type '[]'.
!!! error TS2769: Types of property 'length' are incompatible.
!!! error TS2769: Type 'number' is not assignable to type '0'.
!!! related TS6502 /.ts/lib.es5.d.ts:1350:24: The expected type comes from the return type of this signature.
!!! related TS6502 /.ts/lib.es5.d.ts:1356:27: The expected type comes from the return type of this signature.
!!! related TS6502 /.ts/lib.es5.d.ts:1373:24: The expected type comes from the return type of this signature.
!!! related TS6502 /.ts/lib.es5.d.ts:1379: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
31 changes: 28 additions & 3 deletions tests/baselines/reference/json.stringify.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,40 @@
//// [json.stringify.ts]
var replacer = (k) => undefined;
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, replacer, 2);
JSON.stringify(value, undefined, 2);
JSON.stringify(undefined);
JSON.stringify(undefined, replacer);
JSON.stringify(() => '');
JSON.stringify(() => '', replacer);
JSON.stringify({});
JSON.stringify({}, replacer);
JSON.stringify(new Object());
JSON.stringify(new Object(), replacer);
var anyValue: any;
JSON.stringify(anyValue);
JSON.stringify(anyValue, replacer);


//// [json.stringify.js]
var replacer = function (k) { return undefined; };
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, replacer, 2);
JSON.stringify(value, undefined, 2);
JSON.stringify(undefined);
JSON.stringify(undefined, replacer);
JSON.stringify(function () { return ''; });
JSON.stringify(function () { return ''; }, replacer);
JSON.stringify({});
JSON.stringify({}, replacer);
JSON.stringify(new Object());
JSON.stringify(new Object(), replacer);
var anyValue;
JSON.stringify(anyValue);
JSON.stringify(anyValue, replacer);
Loading

0 comments on commit f26c011

Please sign in to comment.