Skip to content

fix(react): make wrapCreateBrowserRouter generic #6862

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/react/src/reactrouterv6.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,13 @@ export function wrapUseRoutes(origUseRoutes: UseRoutes): UseRoutes {
};
}

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

// The initial load ends when `createBrowserRouter` is called.
Expand Down
50 changes: 12 additions & 38 deletions packages/react/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ export interface Path {
hash: string;
}

export interface RouterSubscriber {
(state: RouterState): void;
export interface RouterSubscriber<TState extends RouterState = RouterState> {
(state: TState): void;
}
export interface GetScrollPositionFunction {
(): number;
Expand Down Expand Up @@ -204,39 +204,13 @@ export declare enum HistoryAction {
export interface RouterState {
historyAction: Action | HistoryAction | any;
location: any;
matches: AgnosticDataRouteMatch[];
initialized: boolean;
navigation: Navigation;
restoreScrollPosition: number | false | null;
preventScrollReset: boolean;
revalidation: any;
loaderData: RouteData;
actionData: RouteData | null;
errors: RouteData | null;
fetchers: Map<string, Fetcher>;
}
export interface Router {
basename: string | undefined;
state: RouterState;
routes: AgnosticDataRouteObject[];
_internalFetchControllers: any;
_internalActiveDeferreds: any;
initialize(): Router;
subscribe(fn: RouterSubscriber): () => void;
enableScrollRestoration(
savedScrollPositions: Record<string, number>,
getScrollPosition: GetScrollPositionFunction,
getKey?: any,
): () => void;
navigate(to: number): void;
navigate(to: To, opts?: RouterNavigateOptions): void;
fetch(key: string, routeId: string, href: string, opts?: RouterNavigateOptions): void;
revalidate(): void;
createHref(location: Location | URL): string;
getFetcher(key?: string): any;
deleteFetcher(key?: string): void;
dispose(): void;
encodeLocation(to: To): Path;
}

export type CreateRouterFunction = (routes: RouteObject[], opts?: any) => Router;
}
export interface Router<TState extends RouterState = RouterState> {
state: TState;
subscribe(fn: RouterSubscriber<TState>): () => void;
}

export type CreateRouterFunction<
TState extends RouterState = RouterState,
TRouter extends Router<TState> = Router<TState>,
> = (routes: RouteObject[], opts?: any) => TRouter;
2 changes: 1 addition & 1 deletion packages/react/test/reactrouterv6.4.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
useNavigationType,
} from 'react-router-6.4';

import { reactRouterV6Instrumentation,wrapCreateBrowserRouter } from '../src';
import { reactRouterV6Instrumentation, wrapCreateBrowserRouter } from '../src';
import type { CreateRouterFunction } from '../src/types';

beforeAll(() => {
Expand Down