Skip to content

Commit

Permalink
fix: JSON.parse accepting null
Browse files Browse the repository at this point in the history
  • Loading branch information
ImLunaHey committed Oct 18, 2023
1 parent b2df073 commit 9ab1a8f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/entrypoints/json-parse.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
interface JSON {
/**
* Converts a JavaScript Object Notation (JSON) string into an object.
* @param text A valid JSON string.
* @param value A valid JSON string or null.
* @param reviver A function that transforms the results. This function is called for each member of the object.
* If a member contains nested objects, the nested objects are transformed before the parent object is.
*/
parse(
text: string,
value: string,
reviver?: (this: any, key: string, value: any) => any,
): unknown;
parse(
value: null,
reviver?: (this: any, key: string, value: any) => any,
): null;
parse(
value: string | null,
reviver?: (this: any, key: string, value: any) => any,
): unknown;
}
13 changes: 13 additions & 0 deletions src/tests/json-parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,16 @@ doNotExecute(() => {
// @ts-expect-error
const result = JSON.parse<string>("{}");
});

doNotExecute(() => {
const result = JSON.parse(null);

type tests = [Expect<Equal<typeof result, null>>];
});

doNotExecute(() => {
const func = (): string | null => null;
const result = JSON.parse(func());

type tests = [Expect<Equal<typeof result, unknown>>];
});

0 comments on commit 9ab1a8f

Please sign in to comment.