From 5aac41638cfa0eabee73000326d976d4d58613da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20De=20Boey?= Date: Sun, 22 Oct 2023 18:24:41 +0200 Subject: [PATCH] fix(remix-server-runtime): fix `Jsonify` for objects with `toJSON` function --- packages/remix-server-runtime/jsonify.ts | 34 ++++++++++++++++++++---- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/packages/remix-server-runtime/jsonify.ts b/packages/remix-server-runtime/jsonify.ts index 80e524ff573..132452fb57b 100644 --- a/packages/remix-server-runtime/jsonify.ts +++ b/packages/remix-server-runtime/jsonify.ts @@ -13,7 +13,7 @@ export type Jsonify = IsAny extends true ? any : // toJSON - T extends { toJSON(): infer U } ? (U extends JsonValue ? U : unknown) : + T extends { toJSON(): infer U } ? (U extends JsonValue ? U : Jsonify) : // primitives T extends JsonPrimitive ? T : @@ -49,7 +49,7 @@ export type Jsonify = // value is always not JSON => true // value is always JSON => false -// value is somtimes JSON, sometimes not JSON => boolean +// value is sometimes JSON, sometimes not JSON => boolean // note: cannot be inlined as logic requires union distribution type ValueIsNotJson = T extends NotJson ? true : false; @@ -150,8 +150,15 @@ type _tests = [ // toJson Expect, "stuff">>, Expect, string>>, - Expect, unknown>>, - Expect, unknown>>, + Expect, any>>, + Expect, never>>, + Expect, string>>, + Expect }>, EmptyObject>>, + Expect, Record>>, + Expect, [1, "two", string, null, false]>>, + Expect, [unknown, 1]>>, + // @ts-expect-error + Expect, {a: string}>>, Expect, string>>, @@ -164,14 +171,31 @@ type _tests = [ // object Expect>, {}>>, + // @ts-expect-error + Expect>, {a: any}>>, Expect>, {a: string}>>, + Expect>, {a: string}>>, + Expect}>>, {a: EmptyObject}>>, + Expect>, {a: Record}>>, + Expect>, {a: [1, "two", string, null, false]}>>, + Expect>, {a: [unknown, 1]}>>, + // @ts-expect-error + Expect>, {a: {a: string}}>>, Expect>, {a?: string}>>, Expect>, {a?: string}>>, + Expect>, {a?: any}>>, + Expect>, {a?: string}>>, + Expect}>>, {a?: EmptyObject}>>, + Expect>, {a?: Record}>>, + Expect>, {a?: [1, "two", string, null, false]}>>, + Expect>, {a?: [unknown, 1]}>>, + // @ts-expect-error + Expect>, {a?: {a: string}}>>, Expect>, {a: string, b?: string}>>, Expect>, {}>>, Expect>>, Record>>, Expect>>, Record>>, - Expect}>, { payload: Record}>>, + Expect}>, {payload: Record}>>, Expect