Skip to content

Commit c40f786

Browse files
authored
Update LoaderFunctionArgs and friends when middleware is enabled (#13381)
1 parent 710626f commit c40f786

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

.changeset/violet-carrots-work.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"react-router": patch
3+
---
4+
5+
UNSTABLE: Update context type for `LoaderFunctionArgs`/`ActionFunctionArgs` when middleware is enabled

packages/react-router/lib/router/utils.ts

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { MiddlewareEnabled } from "../types/future";
12
import type { Equal, Expect } from "../types/utils";
23
import type { Location, Path, To } from "./history";
34
import { invariant, parsePath, warning } from "./history";
@@ -172,6 +173,10 @@ export class unstable_RouterContextProvider {
172173
}
173174
}
174175

176+
type DefaultContext = MiddlewareEnabled extends true
177+
? unstable_RouterContextProvider
178+
: any;
179+
175180
/**
176181
* @private
177182
* Arguments passed to route loader/action functions. Same for now but we keep
@@ -225,13 +230,13 @@ export type unstable_MiddlewareFunction<Result = unknown> = (
225230
/**
226231
* Arguments passed to loader functions
227232
*/
228-
export interface LoaderFunctionArgs<Context = any>
233+
export interface LoaderFunctionArgs<Context = DefaultContext>
229234
extends DataFunctionArgs<Context> {}
230235

231236
/**
232237
* Arguments passed to action functions
233238
*/
234-
export interface ActionFunctionArgs<Context = any>
239+
export interface ActionFunctionArgs<Context = DefaultContext>
235240
extends DataFunctionArgs<Context> {}
236241

237242
/**
@@ -244,7 +249,7 @@ type DataFunctionReturnValue = MaybePromise<DataFunctionValue>;
244249
/**
245250
* Route loader function signature
246251
*/
247-
export type LoaderFunction<Context = any> = {
252+
export type LoaderFunction<Context = DefaultContext> = {
248253
(
249254
args: LoaderFunctionArgs<Context>,
250255
handlerCtx?: unknown
@@ -254,7 +259,7 @@ export type LoaderFunction<Context = any> = {
254259
/**
255260
* Route action function signature
256261
*/
257-
export interface ActionFunction<Context = any> {
262+
export interface ActionFunction<Context = DefaultContext> {
258263
(
259264
args: ActionFunctionArgs<Context>,
260265
handlerCtx?: unknown
@@ -354,7 +359,7 @@ export interface DataStrategyMatch
354359
) => Promise<DataStrategyResult>;
355360
}
356361

357-
export interface DataStrategyFunctionArgs<Context = any>
362+
export interface DataStrategyFunctionArgs<Context = DefaultContext>
358363
extends DataFunctionArgs<Context> {
359364
matches: DataStrategyMatch[];
360365
// TODO: Implement
@@ -370,7 +375,7 @@ export interface DataStrategyResult {
370375
result: unknown; // data, Error, Response, DeferredData, DataWithResponseInit
371376
}
372377

373-
export interface DataStrategyFunction<Context = any> {
378+
export interface DataStrategyFunction<Context = DefaultContext> {
374379
(args: DataStrategyFunctionArgs<Context>): Promise<
375380
Record<string, DataStrategyResult>
376381
>;

0 commit comments

Comments
 (0)