Skip to content

Commit 868c20b

Browse files
authored
fix(react): make wrapCreateBrowserRouter generic (#6862)
Reduces the Router and RouterState to the minimal internal usage, and makes the wrapper accept a generic which extends the minimal types. This allows for react-router to make changes to their Router types, without breaking our types.
1 parent ae56638 commit 868c20b

File tree

3 files changed

+18
-41
lines changed

3 files changed

+18
-41
lines changed

packages/react/src/reactrouterv6.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,13 @@ export function wrapUseRoutes(origUseRoutes: UseRoutes): UseRoutes {
249249
};
250250
}
251251

252-
export function wrapCreateBrowserRouter(createRouterFunction: CreateRouterFunction): CreateRouterFunction {
252+
export function wrapCreateBrowserRouter<
253+
TState extends RouterState = RouterState,
254+
TRouter extends Router<TState> = Router<TState>,
255+
>(createRouterFunction: CreateRouterFunction<TState, TRouter>): CreateRouterFunction<TState, TRouter> {
253256
// `opts` for createBrowserHistory and createMemoryHistory are different, but also not relevant for us at the moment.
254257
// eslint-disable-next-line @typescript-eslint/no-explicit-any
255-
return function (routes: RouteObject[], opts?: any): Router {
258+
return function (routes: RouteObject[], opts?: any): TRouter {
256259
const router = createRouterFunction(routes, opts);
257260

258261
// The initial load ends when `createBrowserRouter` is called.

packages/react/src/types.ts

Lines changed: 12 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ export interface Path {
148148
hash: string;
149149
}
150150

151-
export interface RouterSubscriber {
152-
(state: RouterState): void;
151+
export interface RouterSubscriber<TState extends RouterState = RouterState> {
152+
(state: TState): void;
153153
}
154154
export interface GetScrollPositionFunction {
155155
(): number;
@@ -204,39 +204,13 @@ export declare enum HistoryAction {
204204
export interface RouterState {
205205
historyAction: Action | HistoryAction | any;
206206
location: any;
207-
matches: AgnosticDataRouteMatch[];
208-
initialized: boolean;
209-
navigation: Navigation;
210-
restoreScrollPosition: number | false | null;
211-
preventScrollReset: boolean;
212-
revalidation: any;
213-
loaderData: RouteData;
214-
actionData: RouteData | null;
215-
errors: RouteData | null;
216-
fetchers: Map<string, Fetcher>;
217-
}
218-
export interface Router {
219-
basename: string | undefined;
220-
state: RouterState;
221-
routes: AgnosticDataRouteObject[];
222-
_internalFetchControllers: any;
223-
_internalActiveDeferreds: any;
224-
initialize(): Router;
225-
subscribe(fn: RouterSubscriber): () => void;
226-
enableScrollRestoration(
227-
savedScrollPositions: Record<string, number>,
228-
getScrollPosition: GetScrollPositionFunction,
229-
getKey?: any,
230-
): () => void;
231-
navigate(to: number): void;
232-
navigate(to: To, opts?: RouterNavigateOptions): void;
233-
fetch(key: string, routeId: string, href: string, opts?: RouterNavigateOptions): void;
234-
revalidate(): void;
235-
createHref(location: Location | URL): string;
236-
getFetcher(key?: string): any;
237-
deleteFetcher(key?: string): void;
238-
dispose(): void;
239-
encodeLocation(to: To): Path;
240-
}
241-
242-
export type CreateRouterFunction = (routes: RouteObject[], opts?: any) => Router;
207+
}
208+
export interface Router<TState extends RouterState = RouterState> {
209+
state: TState;
210+
subscribe(fn: RouterSubscriber<TState>): () => void;
211+
}
212+
213+
export type CreateRouterFunction<
214+
TState extends RouterState = RouterState,
215+
TRouter extends Router<TState> = Router<TState>,
216+
> = (routes: RouteObject[], opts?: any) => TRouter;

packages/react/test/reactrouterv6.4.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
useNavigationType,
1313
} from 'react-router-6.4';
1414

15-
import { reactRouterV6Instrumentation,wrapCreateBrowserRouter } from '../src';
15+
import { reactRouterV6Instrumentation, wrapCreateBrowserRouter } from '../src';
1616
import type { CreateRouterFunction } from '../src/types';
1717

1818
beforeAll(() => {

0 commit comments

Comments
 (0)