Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix single-fetch types for functions that return unions #9999

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 }
>>,
]