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: JSON.parse accepting null #174

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/entrypoints/json-parse.d.ts
@@ -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
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>>];
});