Skip to content

Commit

Permalink
fix remix-run#9826: support conditional return types from loader when…
Browse files Browse the repository at this point in the history
… serializing
  • Loading branch information
michaelgmcd committed Aug 5, 2024
1 parent 907bcdb commit 248702e
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions packages/remix-server-runtime/single-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,16 +369,25 @@ type DataFunctionReturnValue =
| TypedDeferredData<Record<string, unknown>>
| TypedResponse<Record<string, unknown>>;

type Unwrap<T extends DataFunctionReturnValue> = T extends TypedDeferredData<
infer D
>
? D
: T extends TypedResponse<Record<string, unknown>>
? SerializeFrom<T>
: T extends DataWithResponseInit<infer D>
? D
: 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 Serialize<T extends Loader | Action> =
Awaited<ReturnType<T>> extends TypedDeferredData<infer D> ? D :
Awaited<ReturnType<T>> extends TypedResponse<Record<string, unknown>> ? SerializeFrom<T> :
Awaited<ReturnType<T>> extends DataWithResponseInit<infer D> ? D :
Awaited<ReturnType<T>>;
export type Serialize<T extends Loader | Action, R = Awaited<ReturnType<T>>> =
R extends DataFunctionReturnValue
? Unwrap<R>
: R;

export type Loader = (
args: LoaderFunctionArgs
Expand Down

0 comments on commit 248702e

Please sign in to comment.