You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -145,145 +145,120 @@ Without Single Fetch, any plain Javascript object returned from a `loader` or `a
145
145
146
146
With Single Fetch, naked objects will be streamed directly, so the built-in type inference is no longer accurate once you have opted-into Single Fetch. For example, they would assume that a `Date` would be serialized to a string on the client 😕.
147
147
148
-
In order to ensure you get the proper types when using Single Fetch, we've included a set of type overrides that you can include in your `tsconfig.json`'s `compilerOptions.types` array which aligns the types with the Single Fetch behavior:
149
-
150
-
```json
151
-
{
152
-
"compilerOptions": {
153
-
//...
154
-
"types": [
155
-
// ...
156
-
"@remix-run/react/future/single-fetch.d.ts"
157
-
]
148
+
#### Enable Single Fetch types
149
+
150
+
To switch over to Single Fetch types, you should augment Remix's `Future` interface with `unstable_singleFetch: true`:
151
+
152
+
```ts filename=vite.config.ts
153
+
declaremodule"@remix-run/server-runtime" {
154
+
interfaceFuture {
155
+
unstable_singleFetch:true;
158
156
}
159
157
}
160
158
```
161
159
162
-
🚨 Make sure the single-fetch types come after any other Remix packages in `types` so that they override those existing types.
163
-
164
-
#### Loader/Action Definition Utilities
165
-
166
-
To enhance type-safety when defining loaders and actions with Single Fetch, you can use the new `unstable_defineLoader` and `unstable_defineAction` utilities:
160
+
Now `useLoaderData`, `useActionData`, and any other utilities that use a `typeof loader` generic should be using Single Fetch types:
Not only does this give you types for arguments (and deprecates `LoaderFunctionArgs`), but it also ensures you are returning single-fetch compatible types:
178
+
#### Functions and class instances
177
179
178
-
```ts
179
-
exportconst loader =defineLoader(() => {
180
-
return { hello: "world", badData: () =>1 };
181
-
// ^^^^^^^ Type error: `badData` is not serializable
182
-
});
180
+
In general, functions cannot be reliably sent over the network, so they get serialized as `undefined`:
There are also client-side equivalents un `defineClientLoader`/`defineClientAction` that don't have the same return value restrictions because data returned from `clientLoader`/`clientAction` does not need to be serialized over the wire:
199
+
Methods are also not serializable, so class instances get slimmed down to just their serializable properties:
<docs-info>These utilities are primarily for type inference on `useLoaderData` and its equivalents. If you have a resource route that returns a `Response` and is not consumed by Remix APIs (such as `useFetcher`), then you can just stick with your normal `loader`/`action` definitions. Converting those routes to use `defineLoader`/`defineAction` would cause type errors because `turbo-stream` cannot serialize a `Response` instance.</docs-info>
`useMatches` requires a manual cast to specify the loader type in order to get proper type inference on a given `match.data`. When using Single Fetch, you will need to replace the `UIMatch` type with `UIMatch_SingleFetch`:
234
+
<docs-warning>Make sure to include types for the `clientLoader` args and `clientAction` args as that is how our types detect client data functions.</docs-warning>
268
235
269
-
```diff
270
-
let matches = useMatches();
271
-
- let rootMatch = matches[0] as UIMatch<typeof loader>;
272
-
+ let rootMatch = matches[0] as UIMatch_SingleFetch<typeof loader>;
273
-
```
236
+
Data from client-side loaders and actions are never serialized so types for those are preserved:
274
237
275
-
#### `meta` Function
238
+
```ts
239
+
import {
240
+
useLoaderData,
241
+
typeClientLoaderFunctionArgs,
242
+
} from"@remix-run/react";
276
243
277
-
`meta` functions also require a generic to indicate the current and ancestor route loader types in order to properly type the `data` and `matches` parameters. When using Single Fetch, you will need to replace the `MetaArgs` type with `MetaArgs_SingleFetch`:
0 commit comments