1
1
import type { MetaArgs , UIMatch , UNSAFE_MetaMatch } from "@remix-run/react" ;
2
2
import type {
3
- LoaderFunctionArgs ,
4
- ActionFunctionArgs ,
3
+ Loader ,
4
+ Action ,
5
5
SerializeFrom ,
6
6
TypedDeferredData ,
7
7
TypedResponse ,
@@ -11,90 +11,52 @@ import type {
11
11
FetcherWithComponents ,
12
12
} from "react-router-dom" ;
13
13
14
- type Serializable =
15
- | undefined
16
- | null
17
- | boolean
18
- | string
19
- | symbol
20
- | number
21
- | Array < Serializable >
22
- | { [ key : PropertyKey ] : Serializable }
23
- | bigint
24
- | Date
25
- | URL
26
- | RegExp
27
- | Error
28
- | Map < Serializable , Serializable >
29
- | Set < Serializable >
30
- | Promise < Serializable > ;
31
-
32
- type DataFunctionReturnValue =
33
- | Serializable
34
- | TypedDeferredData < Record < string , unknown > >
35
- | TypedResponse < Record < string , unknown > > ;
36
-
37
- type LoaderFunction_SingleFetch = (
38
- args : LoaderFunctionArgs
39
- ) => Promise < DataFunctionReturnValue > | DataFunctionReturnValue ;
40
- type ActionFunction_SingleFetch = (
41
- args : ActionFunctionArgs
42
- ) => Promise < DataFunctionReturnValue > | DataFunctionReturnValue ;
43
-
44
14
// Backwards-compatible type for Remix v2 where json/defer still use the old types,
45
15
// and only non-json/defer returns use the new types. This allows for incremental
46
16
// migration of loaders to return naked objects. In the next major version,
47
17
// json/defer will be removed so everything will use the new simplified typings.
48
18
// prettier-ignore
49
- type SingleFetchSerialize_V2 < T extends LoaderFunction_SingleFetch | ActionFunction_SingleFetch > =
19
+ type Serialize < T extends Loader | Action > =
50
20
Awaited < ReturnType < T > > extends TypedDeferredData < infer D > ? D :
51
21
Awaited < ReturnType < T > > extends TypedResponse < Record < string , unknown > > ? SerializeFrom < T > :
52
22
Awaited < ReturnType < T > > ;
53
23
54
24
declare module "@remix-run/react" {
55
- export function useLoaderData < T > ( ) : T extends LoaderFunction_SingleFetch
56
- ? SingleFetchSerialize_V2 < T >
57
- : never ;
25
+ export function useLoaderData < T > ( ) : T extends Loader ? Serialize < T > : T ;
58
26
59
- export function useActionData < T > ( ) : T extends ActionFunction_SingleFetch
60
- ? SingleFetchSerialize_V2 < T > | undefined
61
- : never ;
27
+ export function useActionData < T > ( ) : T extends Action
28
+ ? Serialize < T > | undefined
29
+ : T ;
62
30
63
31
export function useRouteLoaderData < T > (
64
32
routeId : string
65
- ) : T extends LoaderFunction_SingleFetch ? SingleFetchSerialize_V2 < T > : never ;
33
+ ) : T extends Loader ? Serialize < T > : never ;
66
34
67
35
export function useFetcher < TData = unknown > (
68
36
opts ?: Parameters < typeof useFetcherRR > [ 0 ]
69
37
) : FetcherWithComponents <
70
- TData extends LoaderFunction_SingleFetch | ActionFunction_SingleFetch
71
- ? SingleFetchSerialize_V2 < TData >
72
- : never
38
+ TData extends Loader | Action ? Serialize < TData > : TData
73
39
> ;
74
40
75
41
export type UIMatch_SingleFetch < D = unknown , H = unknown > = Omit <
76
42
UIMatch < D , H > ,
77
43
"data"
78
44
> & {
79
- data : D extends LoaderFunction_SingleFetch
80
- ? SingleFetchSerialize_V2 < D >
81
- : never ;
45
+ data : D extends Loader ? Serialize < D > : never ;
82
46
} ;
83
47
84
48
interface MetaMatch_SingleFetch <
85
49
RouteId extends string = string ,
86
- Loader extends LoaderFunction_SingleFetch | unknown = unknown
87
- > extends Omit < UNSAFE_MetaMatch < RouteId , Loader > , "data" > {
88
- data : Loader extends LoaderFunction_SingleFetch
89
- ? SingleFetchSerialize_V2 < Loader >
90
- : unknown ;
50
+ L extends Loader | unknown = unknown
51
+ > extends Omit < UNSAFE_MetaMatch < RouteId , L > , "data" > {
52
+ data : L extends Loader ? Serialize < L > : unknown ;
91
53
}
92
54
93
55
type MetaMatches_SingleFetch <
94
- MatchLoaders extends Record <
56
+ MatchLoaders extends Record < string , Loader | unknown > = Record <
95
57
string ,
96
- LoaderFunction_SingleFetch | unknown
97
- > = Record < string , unknown >
58
+ unknown
59
+ >
98
60
> = Array <
99
61
{
100
62
[ K in keyof MatchLoaders ] : MetaMatch_SingleFetch <
@@ -105,17 +67,13 @@ declare module "@remix-run/react" {
105
67
> ;
106
68
107
69
export interface MetaArgs_SingleFetch <
108
- Loader extends LoaderFunction_SingleFetch | unknown = unknown ,
109
- MatchLoaders extends Record <
70
+ L extends Loader | unknown = unknown ,
71
+ MatchLoaders extends Record < string , Loader | unknown > = Record <
110
72
string ,
111
- LoaderFunction_SingleFetch | unknown
112
- > = Record < string , unknown >
113
- > extends Omit < MetaArgs < Loader , MatchLoaders > , "data" | "matches" > {
114
- data :
115
- | ( Loader extends LoaderFunction_SingleFetch
116
- ? SingleFetchSerialize_V2 < Loader >
117
- : unknown )
118
- | undefined ;
73
+ unknown
74
+ >
75
+ > extends Omit < MetaArgs < L , MatchLoaders > , "data" | "matches" > {
76
+ data : ( L extends Loader ? Serialize < L > : unknown ) | undefined ;
119
77
matches : MetaMatches_SingleFetch < MatchLoaders > ;
120
78
}
121
79
}
0 commit comments