Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

opt-in type inference for single-fetch #9272

Merged
merged 8 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
34 changes: 34 additions & 0 deletions .changeset/young-eagles-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
"@remix-run/react": patch
---

Opt-in types for single-fetch

To opt-in to type inference for single-fetch, add `future/single-fetch.d.ts` to `include` in your `tsconfig.json`:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like this API.


```json
{
"include": [
"./node_modules/@remix-run/react/future/single-fetch.d.ts"
]
}
```

This changes `useLoaderData` and `useActionData` types to return single-fetch aware types instead of `SerializedFrom` types:


```ts
const loader = () => {
return { hello: "world", date: new Date() }
}

// Without opting into single-fetch types
// Types from `loader` are serialized via `JSON.stringify` and `JSON.parse`
const before = useLoaderData<typeof loader>();
// ^? { hello: string, date: string }

// Opting into single-fetch types
// Types from `loader` are serialized via `turbo-stream`
const after = useLoaderData<typeof loader>();
// ^? { hello: string, date: Date }
```
51 changes: 51 additions & 0 deletions packages/remix-react/future/single-fetch.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import type { AppLoadContext } from "@remix-run/server-runtime";

type Serializable =
| undefined
| null
| boolean
| string
| symbol
| number
| Array<Serializable>
| { [key: PropertyKey]: Serializable }
| bigint
| Date
| URL
| RegExp
| Error
| Map<Serializable, Serializable>
| Set<Serializable>
| Promise<Serializable>;

type Params<Key extends string = string> = {
readonly [key in Key]: string | undefined;
};

type ResponseStub = {
status?: number;
headers: Headers;
};

type DataFunction = (
args: {
request: Request;
params: Params;
context: AppLoadContext;
response: ResponseStub;
},
handlerCtx?: unknown
) => Serializable;

type Loader = DataFunction & { hydrate?: boolean };
type Action = DataFunction;

declare module "@remix-run/react" {
export function useLoaderData<T>(): T extends Loader
? Awaited<ReturnType<T>>
: never;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will need to fork in here and use the old SerializeFrom types if a user returns a Response from the loader/actionsince that will still go through res.json()

export function useActionData<T>(): T extends Action
? Awaited<ReturnType<T>>
: never;
}
1 change: 1 addition & 0 deletions packages/remix-react/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ module.exports = function rollup() {
{ src: "LICENSE.md", dest: [outputDir, sourceDir] },
{ src: `${sourceDir}/package.json`, dest: outputDir },
{ src: `${sourceDir}/README.md`, dest: outputDir },
{ src: `${sourceDir}/future`, dest: outputDir },
],
}),
copyToPlaygrounds(),
Expand Down