Skip to content

Commit 8355264

Browse files
committed
feat: patch Await
1 parent 78b42df commit 8355264

File tree

8 files changed

+55
-1
lines changed

8 files changed

+55
-1
lines changed

.changeset/perfect-tools-compete.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'react-router-typesafe': minor
3+
---
4+
5+
Patch <Await> component

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,22 @@ const action = makeAction(() => ({ ok: true }));
5555

5656
Feel free to improve the code and submit a pull request. If you're not sure about something, create an issue first to discuss it.
5757

58-
## Patched functions
58+
## Functions
5959

6060
| Status | Utility | Before | After |
6161
| ------ | -------------------- | ---------- | ------------------------------------------------------------ |
6262
|| `defer` | `Response` | Generic matching the first argument |
6363
|| `useLoaderData` | `unknown` | Generic function with the type of the loader function passed |
6464
|| `useActionData` | `unknown` | Generic function with the type of the action function passed |
6565
|| `useRouteLoaderData` | `unknown` | Generic function with the type of the loader function passed |
66+
| NEW | `makeLoader` | `unknown` | Wrapper around `satisfies` for ergonomics |
67+
| NEW | `makeAction` | `unknown` | Wrapper around `satisfies` for ergonomics |
68+
69+
## Patched components
70+
71+
| Status | Component | Before | After |
72+
| ------ | --------- | --------------------------------------------- | --------------------------------------------- |
73+
|| `<Await>` | children render props would be typed as `any` | Generic component makes render props typesafe |
6674

6775
## About
6876

File renamed without changes.

src/components/Await.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Await as RRAwait } from 'react-router-dom';
2+
3+
export type AwaitResolveRenderFunction<T> = {
4+
(data: Awaited<T>): React.ReactNode;
5+
};
6+
7+
export type AwaitProps<T> = {
8+
children: React.ReactNode | AwaitResolveRenderFunction<T>;
9+
errorElement?: React.ReactNode;
10+
resolve: T;
11+
};
12+
13+
export const Await = RRAwait as <T>(props: AwaitProps<T>) => React.ReactNode;

src/components/Await.typetest.tsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { test } from 'bun:test';
2+
import { expectTypeOf } from 'expect-type';
3+
import { Await } from './Await';
4+
5+
test('render props return awaited promise resolved value', () => {
6+
const promiseThatReturnsNumber = Promise.resolve(1);
7+
8+
const rendered = Await({
9+
resolve: promiseThatReturnsNumber,
10+
children: resolved => {
11+
expectTypeOf(resolved).toEqualTypeOf<number>();
12+
return resolved;
13+
},
14+
});
15+
});
16+
17+
test('does not error/warn with non-promises', () => {
18+
const rendered = Await({
19+
resolve: 1,
20+
children: resolved => {
21+
expectTypeOf(resolved).toEqualTypeOf<number>();
22+
return resolved;
23+
},
24+
});
25+
});

src/components/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './Await';

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export { defer } from './defer';
22
export { type LoaderData, useLoaderData } from './loader';
33
export { type ActionData, useActionData } from './action';
44
export { makeLoader, makeAction } from './utils';
5+
export * from './components';
56

67
/** Re-exports for commodity */
78
export { type LoaderFunction, type ActionFunction, redirect } from 'react-router-dom';

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"strict": true,
1818
"noUncheckedIndexedAccess": true,
1919
"noImplicitAny": true,
20+
"jsx": "react-jsx",
2021
"types": ["bun-types"]
2122
},
2223
"exclude": ["node_modules"]

0 commit comments

Comments
 (0)