Skip to content

Commit 8c9e2b6

Browse files
authored
Fix types for PatchRoutesOnNavigationFunction (#11967)
1 parent 09c1978 commit 8c9e2b6

File tree

5 files changed

+34
-14
lines changed

5 files changed

+34
-14
lines changed

.changeset/silly-walls-sit.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"react-router-dom": patch
3+
"react-router": patch
4+
"@remix-run/router": patch
5+
---
6+
7+
- Fix types for `RouteObject` within `PatchRoutesOnNavigationFunction`'s `patch` method so it doesn't expect agnostic route objects passed to `patch`
8+
- Add new `PatchRoutesOnNavigationFunctionArgs` type for convenience

packages/react-router-dom/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ export type {
132132
OutletProps,
133133
Params,
134134
ParamParseKey,
135+
PatchRoutesOnNavigationFunction,
136+
PatchRoutesOnNavigationFunctionArgs,
135137
Path,
136138
PathMatch,
137139
Pathname,
@@ -151,7 +153,6 @@ export type {
151153
ShouldRevalidateFunctionArgs,
152154
To,
153155
UIMatch,
154-
PatchRoutesOnNavigationFunction,
155156
} from "react-router";
156157
export {
157158
AbortedDeferredError,

packages/react-router/index.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import * as React from "react";
22
import type {
33
ActionFunction,
44
ActionFunctionArgs,
5+
AgnosticPatchRoutesOnNavigationFunction,
6+
AgnosticPatchRoutesOnNavigationFunctionArgs,
57
Blocker,
68
BlockerFunction,
79
DataStrategyFunction,
@@ -32,7 +34,6 @@ import type {
3234
ShouldRevalidateFunctionArgs,
3335
To,
3436
UIMatch,
35-
AgnosticPatchRoutesOnNavigationFunction,
3637
} from "@remix-run/router";
3738
import {
3839
AbortedDeferredError,
@@ -233,6 +234,12 @@ export {
233234
useRoutes,
234235
};
235236

237+
export type PatchRoutesOnNavigationFunctionArgs =
238+
AgnosticPatchRoutesOnNavigationFunctionArgs<RouteObject, RouteMatch>;
239+
240+
export type PatchRoutesOnNavigationFunction =
241+
AgnosticPatchRoutesOnNavigationFunction<RouteObject, RouteMatch>;
242+
236243
function mapRouteProperties(route: RouteObject) {
237244
let updates: Partial<RouteObject> & { hasErrorBoundary: boolean } = {
238245
// Note: this check also occurs in createRoutesFromChildren so update
@@ -291,9 +298,6 @@ function mapRouteProperties(route: RouteObject) {
291298
return updates;
292299
}
293300

294-
export interface PatchRoutesOnNavigationFunction
295-
extends AgnosticPatchRoutesOnNavigationFunction<RouteMatch> {}
296-
297301
export function createMemoryRouter(
298302
routes: RouteObject[],
299303
opts?: {

packages/router/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ export type {
77
AgnosticDataRouteObject,
88
AgnosticIndexRouteObject,
99
AgnosticNonIndexRouteObject,
10+
AgnosticPatchRoutesOnNavigationFunction,
11+
AgnosticPatchRoutesOnNavigationFunctionArgs,
1012
AgnosticRouteMatch,
1113
AgnosticRouteObject,
1214
DataStrategyFunction,
@@ -23,7 +25,6 @@ export type {
2325
LoaderFunctionArgs,
2426
ParamParseKey,
2527
Params,
26-
AgnosticPatchRoutesOnNavigationFunction,
2728
PathMatch,
2829
PathParam,
2930
PathPattern,

packages/router/utils.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -255,15 +255,21 @@ export interface DataStrategyFunction {
255255
(args: DataStrategyFunctionArgs): Promise<Record<string, DataStrategyResult>>;
256256
}
257257

258-
export interface AgnosticPatchRoutesOnNavigationFunction<
258+
export type AgnosticPatchRoutesOnNavigationFunctionArgs<
259+
O extends AgnosticRouteObject = AgnosticRouteObject,
259260
M extends AgnosticRouteMatch = AgnosticRouteMatch
260-
> {
261-
(opts: {
262-
path: string;
263-
matches: M[];
264-
patch: (routeId: string | null, children: AgnosticRouteObject[]) => void;
265-
}): void | Promise<void>;
266-
}
261+
> = {
262+
path: string;
263+
matches: M[];
264+
patch: (routeId: string | null, children: O[]) => void;
265+
};
266+
267+
export type AgnosticPatchRoutesOnNavigationFunction<
268+
O extends AgnosticRouteObject = AgnosticRouteObject,
269+
M extends AgnosticRouteMatch = AgnosticRouteMatch
270+
> = (
271+
opts: AgnosticPatchRoutesOnNavigationFunctionArgs<O, M>
272+
) => void | Promise<void>;
267273

268274
/**
269275
* Function provided by the framework-aware layers to set any framework-specific

0 commit comments

Comments
 (0)