diff --git a/.changeset/route-lazy-type.md b/.changeset/route-lazy-type.md new file mode 100644 index 0000000000..10ede70426 --- /dev/null +++ b/.changeset/route-lazy-type.md @@ -0,0 +1,5 @@ +--- +"@remix-run/router": patch +--- + +Enhance the return type of `Route.lazy` to prohibit returning an empty object diff --git a/packages/react-router/lib/context.ts b/packages/react-router/lib/context.ts index 8e5951154c..0cf6b94208 100644 --- a/packages/react-router/lib/context.ts +++ b/packages/react-router/lib/context.ts @@ -10,7 +10,6 @@ import type { StaticHandlerContext, To, TrackedPromise, - LazyRouteFunction, } from "@remix-run/router"; import type { Action as NavigationType } from "@remix-run/router"; @@ -31,7 +30,7 @@ export interface IndexRouteObject { errorElement?: React.ReactNode | null; Component?: React.ComponentType | null; ErrorBoundary?: React.ComponentType | null; - lazy?: LazyRouteFunction; + lazy?: AgnosticIndexRouteObject["lazy"]; } export interface NonIndexRouteObject { @@ -49,7 +48,7 @@ export interface NonIndexRouteObject { errorElement?: React.ReactNode | null; Component?: React.ComponentType | null; ErrorBoundary?: React.ComponentType | null; - lazy?: LazyRouteFunction; + lazy?: AgnosticNonIndexRouteObject["lazy"]; } export type RouteObject = IndexRouteObject | NonIndexRouteObject; diff --git a/packages/router/utils.ts b/packages/router/utils.ts index 6c7796ed3f..14b4126d6a 100644 --- a/packages/router/utils.ts +++ b/packages/router/utils.ts @@ -239,12 +239,19 @@ export const immutableRouteKeys = new Set([ "children", ]); +type RequireOne = Exclude< + { + [K in keyof T]: K extends Key ? Omit & Required> : never; + }[keyof T], + undefined +>; + /** * lazy() function to load a route definition, which can add non-matching * related properties to a route */ export interface LazyRouteFunction { - (): Promise>; + (): Promise>>; } /**