From 311c3f380da7c9bb1a129ba9b27160090bb46b1f Mon Sep 17 00:00:00 2001 From: Christoffer Bjelke Date: Thu, 7 Mar 2024 12:01:26 +0100 Subject: [PATCH] Update JSON.stringify type definition to handle function types --- src/entrypoints/json-stringify.d.ts | 8 ++++++-- src/tests/json-stringify.ts | 8 ++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/entrypoints/json-stringify.d.ts b/src/entrypoints/json-stringify.d.ts index e7ca848..291b394 100644 --- a/src/entrypoints/json-stringify.d.ts +++ b/src/entrypoints/json-stringify.d.ts @@ -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; @@ -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; diff --git a/src/tests/json-stringify.ts b/src/tests/json-stringify.ts index 1120f70..fc142bc 100644 --- a/src/tests/json-stringify.ts +++ b/src/tests/json-stringify.ts @@ -25,6 +25,7 @@ doNotExecute(() => { type tests = [Expect>]; }); + doNotExecute(() => { // create a something that is either an object or undefined let toBeStringified: {} | undefined = Math.random() > 0.5 ? {} : undefined; @@ -33,6 +34,13 @@ doNotExecute(() => { type tests = [Expect>]; }); +doNotExecute(() => { + // create a something that is a function + const result = JSON.stringify(function () {}); + + type tests = [Expect>]; +}); + doNotExecute(() => { // create a something that is of type any let toBeStringified: any;