Skip to content

Commit

Permalink
Update JSON.stringify type definition to handle function types
Browse files Browse the repository at this point in the history
  • Loading branch information
chribjel committed Mar 7, 2024
1 parent 3d460f3 commit 311c3f3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/entrypoints/json-stringify.d.ts
Expand Up @@ -10,7 +10,9 @@ interface JSON {
replacer?: (this: any, key: string, value: any) => any,
space?: string | number,
): T extends {} | null
? string
? T extends () => void
? undefined
: string
: T extends undefined
? undefined
: string | undefined;
Expand All @@ -25,7 +27,9 @@ interface JSON {
replacer?: (number | string)[] | null,
space?: string | number,
): T extends {} | null
? string
? T extends () => void
? undefined
: string
: T extends undefined
? undefined
: string | undefined;
Expand Down
8 changes: 8 additions & 0 deletions src/tests/json-stringify.ts
Expand Up @@ -25,6 +25,7 @@ doNotExecute(() => {

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

doNotExecute(() => {
// create a something that is either an object or undefined
let toBeStringified: {} | undefined = Math.random() > 0.5 ? {} : undefined;
Expand All @@ -33,6 +34,13 @@ doNotExecute(() => {
type tests = [Expect<Equal<typeof result, string | undefined>>];
});

doNotExecute(() => {
// create a something that is a function
const result = JSON.stringify(function () {});

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

doNotExecute(() => {
// create a something that is of type any
let toBeStringified: any;
Expand Down

0 comments on commit 311c3f3

Please sign in to comment.