Skip to content
New issue

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

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

Already on GitHub? # to your account

fix #9826: support conditional return types from loader when serializing #9834

Closed
wants to merge 1 commit into from
Closed
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
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 @@ -387,16 +387,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