Skip to content

Commit

Permalink
opt-in type inference for single-fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
pcattori committed Apr 21, 2024
1 parent 88508f7 commit 7ad6d97
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .changeset/young-eagles-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
"@remix-run/react": patch
---

Opt-in types for single-fetch

In your `tsconfig.json`:

```json
{
"include": [
"./node_modules/@remix-run/react/future/single-fetch.d.ts"
]
}
```
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;

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

0 comments on commit 7ad6d97

Please sign in to comment.