From 8c9e2b62612bd5e84e1c14ef52fd60afe5bb124e Mon Sep 17 00:00:00 2001 From: Matt Brophy Date: Tue, 10 Sep 2024 11:26:24 -0400 Subject: [PATCH] Fix types for PatchRoutesOnNavigationFunction (#11967) --- .changeset/silly-walls-sit.md | 8 ++++++++ packages/react-router-dom/index.tsx | 3 ++- packages/react-router/index.ts | 12 ++++++++---- packages/router/index.ts | 3 ++- packages/router/utils.ts | 22 ++++++++++++++-------- 5 files changed, 34 insertions(+), 14 deletions(-) create mode 100644 .changeset/silly-walls-sit.md diff --git a/.changeset/silly-walls-sit.md b/.changeset/silly-walls-sit.md new file mode 100644 index 0000000000..73dc699e78 --- /dev/null +++ b/.changeset/silly-walls-sit.md @@ -0,0 +1,8 @@ +--- +"react-router-dom": patch +"react-router": patch +"@remix-run/router": patch +--- + +- Fix types for `RouteObject` within `PatchRoutesOnNavigationFunction`'s `patch` method so it doesn't expect agnostic route objects passed to `patch` +- Add new `PatchRoutesOnNavigationFunctionArgs` type for convenience diff --git a/packages/react-router-dom/index.tsx b/packages/react-router-dom/index.tsx index 6b30da81d2..662ed3c88f 100644 --- a/packages/react-router-dom/index.tsx +++ b/packages/react-router-dom/index.tsx @@ -132,6 +132,8 @@ export type { OutletProps, Params, ParamParseKey, + PatchRoutesOnNavigationFunction, + PatchRoutesOnNavigationFunctionArgs, Path, PathMatch, Pathname, @@ -151,7 +153,6 @@ export type { ShouldRevalidateFunctionArgs, To, UIMatch, - PatchRoutesOnNavigationFunction, } from "react-router"; export { AbortedDeferredError, diff --git a/packages/react-router/index.ts b/packages/react-router/index.ts index 79cca46235..19b8ff1631 100644 --- a/packages/react-router/index.ts +++ b/packages/react-router/index.ts @@ -2,6 +2,8 @@ import * as React from "react"; import type { ActionFunction, ActionFunctionArgs, + AgnosticPatchRoutesOnNavigationFunction, + AgnosticPatchRoutesOnNavigationFunctionArgs, Blocker, BlockerFunction, DataStrategyFunction, @@ -32,7 +34,6 @@ import type { ShouldRevalidateFunctionArgs, To, UIMatch, - AgnosticPatchRoutesOnNavigationFunction, } from "@remix-run/router"; import { AbortedDeferredError, @@ -233,6 +234,12 @@ export { useRoutes, }; +export type PatchRoutesOnNavigationFunctionArgs = + AgnosticPatchRoutesOnNavigationFunctionArgs; + +export type PatchRoutesOnNavigationFunction = + AgnosticPatchRoutesOnNavigationFunction; + function mapRouteProperties(route: RouteObject) { let updates: Partial & { hasErrorBoundary: boolean } = { // Note: this check also occurs in createRoutesFromChildren so update @@ -291,9 +298,6 @@ function mapRouteProperties(route: RouteObject) { return updates; } -export interface PatchRoutesOnNavigationFunction - extends AgnosticPatchRoutesOnNavigationFunction {} - export function createMemoryRouter( routes: RouteObject[], opts?: { diff --git a/packages/router/index.ts b/packages/router/index.ts index dbdda02610..d0af25fee3 100644 --- a/packages/router/index.ts +++ b/packages/router/index.ts @@ -7,6 +7,8 @@ export type { AgnosticDataRouteObject, AgnosticIndexRouteObject, AgnosticNonIndexRouteObject, + AgnosticPatchRoutesOnNavigationFunction, + AgnosticPatchRoutesOnNavigationFunctionArgs, AgnosticRouteMatch, AgnosticRouteObject, DataStrategyFunction, @@ -23,7 +25,6 @@ export type { LoaderFunctionArgs, ParamParseKey, Params, - AgnosticPatchRoutesOnNavigationFunction, PathMatch, PathParam, PathPattern, diff --git a/packages/router/utils.ts b/packages/router/utils.ts index c6586aee23..2097f62777 100644 --- a/packages/router/utils.ts +++ b/packages/router/utils.ts @@ -255,15 +255,21 @@ export interface DataStrategyFunction { (args: DataStrategyFunctionArgs): Promise>; } -export interface AgnosticPatchRoutesOnNavigationFunction< +export type AgnosticPatchRoutesOnNavigationFunctionArgs< + O extends AgnosticRouteObject = AgnosticRouteObject, M extends AgnosticRouteMatch = AgnosticRouteMatch -> { - (opts: { - path: string; - matches: M[]; - patch: (routeId: string | null, children: AgnosticRouteObject[]) => void; - }): void | Promise; -} +> = { + path: string; + matches: M[]; + patch: (routeId: string | null, children: O[]) => void; +}; + +export type AgnosticPatchRoutesOnNavigationFunction< + O extends AgnosticRouteObject = AgnosticRouteObject, + M extends AgnosticRouteMatch = AgnosticRouteMatch +> = ( + opts: AgnosticPatchRoutesOnNavigationFunctionArgs +) => void | Promise; /** * Function provided by the framework-aware layers to set any framework-specific