From 6edf0364bcbb6197ec0b515580326899b3b41e53 Mon Sep 17 00:00:00 2001 From: Pedro Cattori Date: Tue, 17 Sep 2024 21:33:11 -0400 Subject: [PATCH] fix single-fetch types for functions that return unions Co-authored-by: Michael McDermott --- .changeset/many-donuts-tie.md | 5 +++ packages/remix-server-runtime/single-fetch.ts | 39 ++++++++++++++----- 2 files changed, 34 insertions(+), 10 deletions(-) create mode 100644 .changeset/many-donuts-tie.md diff --git a/.changeset/many-donuts-tie.md b/.changeset/many-donuts-tie.md new file mode 100644 index 00000000000..06923bfd878 --- /dev/null +++ b/.changeset/many-donuts-tie.md @@ -0,0 +1,5 @@ +--- +"@remix-run/server-runtime": patch +--- + +Fix single-fetch types when `loader`, `action`, `clientLoader`, or `clientAction` return a mixture of bare objects, `json(...)`, `defer(...)`, and `unstable_data(...)`. diff --git a/packages/remix-server-runtime/single-fetch.ts b/packages/remix-server-runtime/single-fetch.ts index 222f74bb4e2..046201a0ea9 100644 --- a/packages/remix-server-runtime/single-fetch.ts +++ b/packages/remix-server-runtime/single-fetch.ts @@ -418,6 +418,19 @@ type Serialize = undefined +// prettier-ignore +type ClientData = + T extends TypedResponse ? Jsonify : + T extends TypedDeferredData ? U : + T + +// prettier-ignore +type ServerData = + T extends TypedResponse ? Jsonify : + T extends TypedDeferredData ? Serialize : + T extends DataWithResponseInit ? Serialize : + Serialize + // Backwards-compatible type for Remix v2 where json/defer still use the old types, // and only non-json/defer returns use the new types. This allows for incremental // migration of loaders to return naked objects. In the next major version, @@ -425,15 +438,8 @@ type Serialize = // prettier-ignore export type SerializeFrom = T extends (...args: infer Args) => infer Return ? - Args extends [ClientLoaderFunctionArgs | ClientActionFunctionArgs] ? - Awaited extends TypedResponse ? Jsonify : - Awaited extends TypedDeferredData ? U : - Awaited - : - Awaited extends TypedResponse ? Jsonify : - Awaited extends TypedDeferredData ? Serialize : - Awaited extends DataWithResponseInit ? Serialize : - Serialize>> + Args extends [ClientLoaderFunctionArgs | ClientActionFunctionArgs] ? ClientData> : + ServerData> : T @@ -607,5 +613,18 @@ type _tests = [ Expect}>>>>, { a: string, b: Promise }>>, // non-function backcompat - Expect, {a: string, b: Date}>> + Expect, {a: string, b: Date}>>, + + Expect + | TypedDeferredData<{ e: string; f: Promise }> + | DataWithResponseInit<{ g: string; h: Date }> + >>, + | { a: string; b: Date } + | Jsonify<{ c: string; d: Date }> + | { e: string; f: Promise } + | { g: string; h: Date } + >>, ]