Skip to content

Commit

Permalink
Fix types for PatchRoutesOnNavigationFunction (#11967)
Browse files Browse the repository at this point in the history
  • Loading branch information
brophdawg11 authored Sep 10, 2024
1 parent 09c1978 commit 8c9e2b6
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 14 deletions.
8 changes: 8 additions & 0 deletions .changeset/silly-walls-sit.md
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion packages/react-router-dom/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ export type {
OutletProps,
Params,
ParamParseKey,
PatchRoutesOnNavigationFunction,
PatchRoutesOnNavigationFunctionArgs,
Path,
PathMatch,
Pathname,
Expand All @@ -151,7 +153,6 @@ export type {
ShouldRevalidateFunctionArgs,
To,
UIMatch,
PatchRoutesOnNavigationFunction,
} from "react-router";
export {
AbortedDeferredError,
Expand Down
12 changes: 8 additions & 4 deletions packages/react-router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import * as React from "react";
import type {
ActionFunction,
ActionFunctionArgs,
AgnosticPatchRoutesOnNavigationFunction,
AgnosticPatchRoutesOnNavigationFunctionArgs,
Blocker,
BlockerFunction,
DataStrategyFunction,
Expand Down Expand Up @@ -32,7 +34,6 @@ import type {
ShouldRevalidateFunctionArgs,
To,
UIMatch,
AgnosticPatchRoutesOnNavigationFunction,
} from "@remix-run/router";
import {
AbortedDeferredError,
Expand Down Expand Up @@ -233,6 +234,12 @@ export {
useRoutes,
};

export type PatchRoutesOnNavigationFunctionArgs =
AgnosticPatchRoutesOnNavigationFunctionArgs<RouteObject, RouteMatch>;

export type PatchRoutesOnNavigationFunction =
AgnosticPatchRoutesOnNavigationFunction<RouteObject, RouteMatch>;

function mapRouteProperties(route: RouteObject) {
let updates: Partial<RouteObject> & { hasErrorBoundary: boolean } = {
// Note: this check also occurs in createRoutesFromChildren so update
Expand Down Expand Up @@ -291,9 +298,6 @@ function mapRouteProperties(route: RouteObject) {
return updates;
}

export interface PatchRoutesOnNavigationFunction
extends AgnosticPatchRoutesOnNavigationFunction<RouteMatch> {}

export function createMemoryRouter(
routes: RouteObject[],
opts?: {
Expand Down
3 changes: 2 additions & 1 deletion packages/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export type {
AgnosticDataRouteObject,
AgnosticIndexRouteObject,
AgnosticNonIndexRouteObject,
AgnosticPatchRoutesOnNavigationFunction,
AgnosticPatchRoutesOnNavigationFunctionArgs,
AgnosticRouteMatch,
AgnosticRouteObject,
DataStrategyFunction,
Expand All @@ -23,7 +25,6 @@ export type {
LoaderFunctionArgs,
ParamParseKey,
Params,
AgnosticPatchRoutesOnNavigationFunction,
PathMatch,
PathParam,
PathPattern,
Expand Down
22 changes: 14 additions & 8 deletions packages/router/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,15 +255,21 @@ export interface DataStrategyFunction {
(args: DataStrategyFunctionArgs): Promise<Record<string, DataStrategyResult>>;
}

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<void>;
}
> = {
path: string;
matches: M[];
patch: (routeId: string | null, children: O[]) => void;
};

export type AgnosticPatchRoutesOnNavigationFunction<
O extends AgnosticRouteObject = AgnosticRouteObject,
M extends AgnosticRouteMatch = AgnosticRouteMatch
> = (
opts: AgnosticPatchRoutesOnNavigationFunctionArgs<O, M>
) => void | Promise<void>;

/**
* Function provided by the framework-aware layers to set any framework-specific
Expand Down

0 comments on commit 8c9e2b6

Please sign in to comment.