Skip to content

Commit 102c599

Browse files
authored
Enhance route.lazy return type (#10634)
* Enahnce route.lazy return type * Fix upstream type in RR
1 parent 0ffa3dc commit 102c599

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

.changeset/route-lazy-type.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@remix-run/router": patch
3+
---
4+
5+
Enhance the return type of `Route.lazy` to prohibit returning an empty object

packages/react-router/lib/context.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import type {
1010
StaticHandlerContext,
1111
To,
1212
TrackedPromise,
13-
LazyRouteFunction,
1413
} from "@remix-run/router";
1514
import type { Action as NavigationType } from "@remix-run/router";
1615

@@ -31,7 +30,7 @@ export interface IndexRouteObject {
3130
errorElement?: React.ReactNode | null;
3231
Component?: React.ComponentType | null;
3332
ErrorBoundary?: React.ComponentType | null;
34-
lazy?: LazyRouteFunction<IndexRouteObject>;
33+
lazy?: AgnosticIndexRouteObject["lazy"];
3534
}
3635

3736
export interface NonIndexRouteObject {
@@ -49,7 +48,7 @@ export interface NonIndexRouteObject {
4948
errorElement?: React.ReactNode | null;
5049
Component?: React.ComponentType | null;
5150
ErrorBoundary?: React.ComponentType | null;
52-
lazy?: LazyRouteFunction<NonIndexRouteObject>;
51+
lazy?: AgnosticNonIndexRouteObject["lazy"];
5352
}
5453

5554
export type RouteObject = IndexRouteObject | NonIndexRouteObject;

packages/router/utils.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,12 +239,19 @@ export const immutableRouteKeys = new Set<ImmutableRouteKey>([
239239
"children",
240240
]);
241241

242+
type RequireOne<T, Key = keyof T> = Exclude<
243+
{
244+
[K in keyof T]: K extends Key ? Omit<T, K> & Required<Pick<T, K>> : never;
245+
}[keyof T],
246+
undefined
247+
>;
248+
242249
/**
243250
* lazy() function to load a route definition, which can add non-matching
244251
* related properties to a route
245252
*/
246253
export interface LazyRouteFunction<R extends AgnosticRouteObject> {
247-
(): Promise<Omit<R, ImmutableRouteKey>>;
254+
(): Promise<RequireOne<Omit<R, ImmutableRouteKey>>>;
248255
}
249256

250257
/**

0 commit comments

Comments
 (0)