diff --git a/.changeset/violet-carrots-work.md b/.changeset/violet-carrots-work.md new file mode 100644 index 0000000000..127454578d --- /dev/null +++ b/.changeset/violet-carrots-work.md @@ -0,0 +1,5 @@ +--- +"react-router": patch +--- + +UNSTABLE: Update context type for `LoaderFunctionArgs`/`ActionFunctionArgs` when middleware is enabled diff --git a/packages/react-router/lib/router/utils.ts b/packages/react-router/lib/router/utils.ts index c375d3c42c..5ea3da65f7 100644 --- a/packages/react-router/lib/router/utils.ts +++ b/packages/react-router/lib/router/utils.ts @@ -1,3 +1,4 @@ +import type { MiddlewareEnabled } from "../types/future"; import type { Equal, Expect } from "../types/utils"; import type { Location, Path, To } from "./history"; import { invariant, parsePath, warning } from "./history"; @@ -172,6 +173,10 @@ export class unstable_RouterContextProvider { } } +type DefaultContext = MiddlewareEnabled extends true + ? unstable_RouterContextProvider + : any; + /** * @private * Arguments passed to route loader/action functions. Same for now but we keep @@ -225,13 +230,13 @@ export type unstable_MiddlewareFunction = ( /** * Arguments passed to loader functions */ -export interface LoaderFunctionArgs +export interface LoaderFunctionArgs extends DataFunctionArgs {} /** * Arguments passed to action functions */ -export interface ActionFunctionArgs +export interface ActionFunctionArgs extends DataFunctionArgs {} /** @@ -244,7 +249,7 @@ type DataFunctionReturnValue = MaybePromise; /** * Route loader function signature */ -export type LoaderFunction = { +export type LoaderFunction = { ( args: LoaderFunctionArgs, handlerCtx?: unknown @@ -254,7 +259,7 @@ export type LoaderFunction = { /** * Route action function signature */ -export interface ActionFunction { +export interface ActionFunction { ( args: ActionFunctionArgs, handlerCtx?: unknown @@ -339,7 +344,7 @@ export interface DataStrategyMatch ) => Promise; } -export interface DataStrategyFunctionArgs +export interface DataStrategyFunctionArgs extends DataFunctionArgs { matches: DataStrategyMatch[]; fetcherKey: string | null; @@ -353,7 +358,7 @@ export interface DataStrategyResult { result: unknown; // data, Error, Response, DeferredData, DataWithResponseInit } -export interface DataStrategyFunction { +export interface DataStrategyFunction { (args: DataStrategyFunctionArgs): Promise< Record >;