Skip to content

Commit 6e70534

Browse files
fix(react): Update types to match react router 6.4 updates (#5992)
Fixes a type mismatch caused by change remix-run/react-router#9366 in react-router code. Have to use any because of typing changes in the above PR, since we can't be backwards compatible with both versions very easily. Co-authored-by: Philip Atkinson <[email protected]>
1 parent 2d068e9 commit 6e70534

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

packages/react/src/reactrouterv6.tsx

+28-7
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,27 @@ import React from 'react';
88

99
import { Action, Location } from './types';
1010

11-
interface RouteObject {
11+
interface NonIndexRouteObject {
1212
caseSensitive?: boolean;
1313
children?: RouteObject[];
14-
element?: React.ReactNode;
15-
index?: boolean;
14+
element?: React.ReactNode | null;
15+
index?: false;
1616
path?: string;
1717
}
1818

19+
interface IndexRouteObject {
20+
caseSensitive?: boolean;
21+
children?: undefined;
22+
element?: React.ReactNode | null;
23+
index?: true;
24+
path?: string;
25+
}
26+
27+
// This type was originally just `type RouteObject = IndexRouteObject`, but this was changed
28+
// in https://github.com/remix-run/react-router/pull/9366, which was released with `6.4.2`
29+
// See https://github.com/remix-run/react-router/issues/9427 for a discussion on this.
30+
type RouteObject = IndexRouteObject | NonIndexRouteObject;
31+
1932
type Params<Key extends string = string> = {
2033
readonly [key in Key]: string | undefined;
2134
};
@@ -45,8 +58,16 @@ interface RouteMatch<ParamKey extends string = string> {
4558
type UseEffect = (cb: () => void, deps: unknown[]) => void;
4659
type UseLocation = () => Location;
4760
type UseNavigationType = () => Action;
48-
type CreateRoutesFromChildren = (children: JSX.Element[]) => RouteObject[];
49-
type MatchRoutes = (routes: RouteObject[], location: Location) => RouteMatch[] | null;
61+
62+
// For both of these types, use `any` instead of `RouteObject[]` or `RouteMatch[]`.
63+
// Have to do this so we maintain backwards compatability between
64+
// react-router > 6.0.0 and >= 6.4.2.
65+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
66+
type RouteObjectArrayAlias = any;
67+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
68+
type RouteMatchAlias = any;
69+
type CreateRoutesFromChildren = (children: JSX.Element[]) => RouteObjectArrayAlias;
70+
type MatchRoutes = (routes: RouteObjectArrayAlias, location: Location) => RouteMatchAlias[] | null;
5071

5172
let activeTransaction: Transaction | undefined;
5273

@@ -106,7 +127,7 @@ function getNormalizedName(
106127
return [location.pathname, 'url'];
107128
}
108129

109-
const branches = matchRoutes(routes, location);
130+
const branches = matchRoutes(routes, location) as unknown as RouteMatch[];
110131

111132
let pathBuilder = '';
112133
if (branches) {
@@ -209,7 +230,7 @@ export function withSentryReactRouterV6Routing<P extends Record<string, any>, R
209230
_useEffect(() => {
210231
// Performance concern:
211232
// This is repeated when <Routes /> is rendered.
212-
routes = _createRoutesFromChildren(props.children);
233+
routes = _createRoutesFromChildren(props.children) as RouteObject[];
213234
isBaseLocation = true;
214235

215236
updatePageloadTransaction(location, routes);

0 commit comments

Comments
 (0)