Skip to content

Commit

Permalink
fix single-fetch types for functions that return unions (#9999)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael McDermott <michael.g.mcdermott@gmail.com>
  • Loading branch information
pcattori and michaelgmcd authored Sep 18, 2024
1 parent 3d7966f commit 4a95727
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/many-donuts-tie.md
Original file line number Diff line number Diff line change
@@ -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(...)`.
39 changes: 29 additions & 10 deletions packages/remix-server-runtime/single-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,22 +418,28 @@ type Serialize<T> =

undefined

// prettier-ignore
type ClientData<T> =
T extends TypedResponse<infer U> ? Jsonify<U> :
T extends TypedDeferredData<infer U> ? U :
T

// prettier-ignore
type ServerData<T> =
T extends TypedResponse<infer U> ? Jsonify<U> :
T extends TypedDeferredData<infer U> ? Serialize<U> :
T extends DataWithResponseInit<infer U> ? Serialize<U> :
Serialize<T>

// 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,
// json/defer will be removed so everything will use the new simplified typings.
// prettier-ignore
export type SerializeFrom<T> =
T extends (...args: infer Args) => infer Return ?
Args extends [ClientLoaderFunctionArgs | ClientActionFunctionArgs] ?
Awaited<Return> extends TypedResponse<infer U> ? Jsonify<U> :
Awaited<Return> extends TypedDeferredData<infer U> ? U :
Awaited<Return>
:
Awaited<Return> extends TypedResponse<infer U> ? Jsonify<U> :
Awaited<Return> extends TypedDeferredData<infer U> ? Serialize<U> :
Awaited<Return> extends DataWithResponseInit<infer D> ? Serialize<D> :
Serialize<Awaited<ReturnType<T>>>
Args extends [ClientLoaderFunctionArgs | ClientActionFunctionArgs] ? ClientData<Awaited<Return>> :
ServerData<Awaited<Return>>
:
T

Expand Down Expand Up @@ -607,5 +613,18 @@ type _tests = [
Expect<Equal<Pretty<SerializeFrom<ServerLoader<TypedDeferredData<{a: string, b: Promise<Date>}>>>>, { a: string, b: Promise<Date> }>>,

// non-function backcompat
Expect<Equal<SerializeFrom<{a: string, b: Date}>, {a: string, b: Date}>>
Expect<Equal<SerializeFrom<{a: string, b: Date}>, {a: string, b: Date}>>,

Expect<Equal<
SerializeFrom<ServerLoader<
| { a: string; b: Date }
| TypedResponse<{ c: string; d: Date }>
| TypedDeferredData<{ e: string; f: Promise<Date> }>
| DataWithResponseInit<{ g: string; h: Date }>
>>,
| { a: string; b: Date }
| Jsonify<{ c: string; d: Date }>
| { e: string; f: Promise<Date> }
| { g: string; h: Date }
>>,
]

0 comments on commit 4a95727

Please sign in to comment.