Skip to content

Commit

Permalink
Enhance route.lazy return type (#10634)
Browse files Browse the repository at this point in the history
* Enahnce route.lazy return type

* Fix upstream type in RR
  • Loading branch information
brophdawg11 committed Jun 30, 2023
1 parent 0ffa3dc commit 102c599
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/route-lazy-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/router": patch
---

Enhance the return type of `Route.lazy` to prohibit returning an empty object
5 changes: 2 additions & 3 deletions packages/react-router/lib/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import type {
StaticHandlerContext,
To,
TrackedPromise,
LazyRouteFunction,
} from "@remix-run/router";
import type { Action as NavigationType } from "@remix-run/router";

Expand All @@ -31,7 +30,7 @@ export interface IndexRouteObject {
errorElement?: React.ReactNode | null;
Component?: React.ComponentType | null;
ErrorBoundary?: React.ComponentType | null;
lazy?: LazyRouteFunction<IndexRouteObject>;
lazy?: AgnosticIndexRouteObject["lazy"];
}

export interface NonIndexRouteObject {
Expand All @@ -49,7 +48,7 @@ export interface NonIndexRouteObject {
errorElement?: React.ReactNode | null;
Component?: React.ComponentType | null;
ErrorBoundary?: React.ComponentType | null;
lazy?: LazyRouteFunction<NonIndexRouteObject>;
lazy?: AgnosticNonIndexRouteObject["lazy"];
}

export type RouteObject = IndexRouteObject | NonIndexRouteObject;
Expand Down
9 changes: 8 additions & 1 deletion packages/router/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,19 @@ export const immutableRouteKeys = new Set<ImmutableRouteKey>([
"children",
]);

type RequireOne<T, Key = keyof T> = Exclude<
{
[K in keyof T]: K extends Key ? Omit<T, K> & Required<Pick<T, K>> : never;
}[keyof T],
undefined
>;

/**
* lazy() function to load a route definition, which can add non-matching
* related properties to a route
*/
export interface LazyRouteFunction<R extends AgnosticRouteObject> {
(): Promise<Omit<R, ImmutableRouteKey>>;
(): Promise<RequireOne<Omit<R, ImmutableRouteKey>>>;
}

/**
Expand Down

0 comments on commit 102c599

Please sign in to comment.