Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
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