Skip to content

Commit

Permalink
RELEASE: backcompat for generic cast in single-fetch types (#9948)
Browse files Browse the repository at this point in the history
  • Loading branch information
pcattori authored Sep 6, 2024
1 parent 1eed6bf commit 1a97d25
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/gorgeous-toys-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"remix": patch
---

REMOVE: bump prerelease version
26 changes: 15 additions & 11 deletions packages/remix-server-runtime/single-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,23 +418,24 @@ type Serialize<T> =

undefined

type Fn = (...args: any[]) => unknown;

// 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 extends Fn> =
Parameters<T> extends [ClientLoaderFunctionArgs | ClientActionFunctionArgs] ?
Awaited<ReturnType<T>> extends TypedResponse<infer U> ? Jsonify<U> :
Awaited<ReturnType<T>> extends TypedDeferredData<infer U> ? U :
Awaited<ReturnType<T>>
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>>>
:
Awaited<ReturnType<T>> extends TypedResponse<infer U> ? Jsonify<U> :
Awaited<ReturnType<T>> extends TypedDeferredData<infer U> ? Serialize<U> :
Awaited<ReturnType<T>> extends DataWithResponseInit<infer D> ? Serialize<D> :
Serialize<Awaited<ReturnType<T>>>;
T

type ServerLoader<T> = (args: LoaderFunctionArgs) => T;
type ClientLoader<T> = (args: ClientLoaderFunctionArgs) => T;
Expand Down Expand Up @@ -604,4 +605,7 @@ type _tests = [

Expect<Equal<Pretty<SerializeFrom<ServerLoader<TypedResponse<{a: string, b: Date}>>>>, { a: string, b: string }>>,
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}>>
]

0 comments on commit 1a97d25

Please sign in to comment.