Skip to content

Commit cb11a79

Browse files
committed
fix: update JSON.stringify type definitions to include undefined in return types
1 parent d518991 commit cb11a79

File tree

2 files changed

+24
-30
lines changed

2 files changed

+24
-30
lines changed

src/entrypoints/json-stringify.d.ts

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,31 +39,13 @@ interface JSON {
3939
* @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
4040
*/
4141
stringify(
42-
value: { toJSON: () => undefined | ((..._: any) => any) | symbol },
42+
value: undefined | symbol,
4343
replacer?: (number | string)[] | null,
4444
space?: string | number,
4545
): undefined;
4646

4747
stringify(
48-
value: { toJSON: () => string | number | boolean | bigint | object | null },
49-
replacer?: (number | string)[] | null,
50-
space?: string | number,
51-
): string;
52-
53-
stringify(
54-
value: { toJSON: () => any },
55-
replacer?: (number | string)[] | null,
56-
space?: string | number,
57-
): string | undefined;
58-
59-
stringify(
60-
value: undefined | ((..._: any) => any) | symbol,
61-
replacer?: (number | string)[] | null,
62-
space?: string | number,
63-
): undefined;
64-
65-
stringify(
66-
value: string | number | boolean | bigint | object | null,
48+
value: string | number | boolean | bigint | null,
6749
replacer?: (number | string)[] | null,
6850
space?: string | number,
6951
): string;

src/tests/json-stringify.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { doNotExecute, Equal, Expect } from "./utils";
33
doNotExecute(() => {
44
const result = JSON.stringify({});
55

6-
type tests = [Expect<Equal<typeof result, string>>];
6+
type tests = [Expect<Equal<typeof result, string | undefined>>];
77
});
88

99
doNotExecute(() => {
@@ -23,7 +23,7 @@ doNotExecute(() => {
2323
let toBeStringified: {} | null = Math.random() > 0.5 ? {} : null;
2424
const result = JSON.stringify(toBeStringified);
2525

26-
type tests = [Expect<Equal<typeof result, string>>];
26+
type tests = [Expect<Equal<typeof result, string | undefined>>];
2727
});
2828

2929
doNotExecute(() => {
@@ -38,7 +38,19 @@ doNotExecute(() => {
3838
// create a something that is a function
3939
const result = JSON.stringify(function () {});
4040

41-
type tests = [Expect<Equal<typeof result, undefined>>];
41+
type tests = [Expect<Equal<typeof result, string | undefined>>];
42+
});
43+
44+
doNotExecute(() => {
45+
function hello() {}
46+
function addToJSONtoFunction(func: any) {
47+
func.toJSON = () => "foo";
48+
}
49+
addToJSONtoFunction(hello);
50+
51+
const result = JSON.stringify(hello);
52+
53+
type tests = [Expect<Equal<typeof result, string | undefined>>];
4254
});
4355

4456
doNotExecute(() => {
@@ -52,7 +64,7 @@ doNotExecute(() => {
5264
// create a something that is a function
5365
const result = JSON.stringify(function (hello: any, world: any) {});
5466

55-
type tests = [Expect<Equal<typeof result, undefined>>];
67+
type tests = [Expect<Equal<typeof result, string | undefined>>];
5668
});
5769

5870
doNotExecute(() => {
@@ -74,7 +86,7 @@ doNotExecute(() => {
7486
// create a something that is a Date object
7587
const result = JSON.stringify(new Date());
7688

77-
type tests = [Expect<Equal<typeof result, string>>];
89+
type tests = [Expect<Equal<typeof result, string | undefined>>];
7890
});
7991

8092
doNotExecute(() => {
@@ -115,7 +127,7 @@ doNotExecute(() => {
115127
};
116128
const result = JSON.stringify(objWithToJSON);
117129

118-
type tests = [Expect<Equal<typeof result, undefined>>];
130+
type tests = [Expect<Equal<typeof result, string | undefined>>];
119131
});
120132

121133
doNotExecute(() => {
@@ -125,7 +137,7 @@ doNotExecute(() => {
125137
};
126138
const result = JSON.stringify(objWithToJSON);
127139

128-
type tests = [Expect<Equal<typeof result, string>>];
140+
type tests = [Expect<Equal<typeof result, string | undefined>>];
129141
});
130142

131143
doNotExecute(() => {
@@ -135,7 +147,7 @@ doNotExecute(() => {
135147
};
136148
const result = JSON.stringify(objWithToJSON);
137149

138-
type tests = [Expect<Equal<typeof result, undefined>>];
150+
type tests = [Expect<Equal<typeof result, string | undefined>>];
139151
});
140152

141153
doNotExecute(() => {
@@ -145,7 +157,7 @@ doNotExecute(() => {
145157
functionWithToJSON.toJSON = () => "foo";
146158
const result = JSON.stringify(functionWithToJSON);
147159

148-
type tests = [Expect<Equal<typeof result, string>>];
160+
type tests = [Expect<Equal<typeof result, string | undefined>>];
149161
});
150162

151163
doNotExecute(() => {
@@ -163,5 +175,5 @@ doNotExecute(() => {
163175
// Date has a toJSON method
164176
const result = JSON.stringify(new Date());
165177

166-
type tests = [Expect<Equal<typeof result, string>>];
178+
type tests = [Expect<Equal<typeof result, string | undefined>>];
167179
});

0 commit comments

Comments
 (0)