From de5e9ecaf22ad98bcfdeeaffd1e25681965d8cb1 Mon Sep 17 00:00:00 2001 From: Tanner Linsley Date: Wed, 30 Aug 2023 11:30:26 -0600 Subject: [PATCH] fix: Better RouteMatch types --- packages/router-core/src/router.ts | 43 +++++++++++++----------------- 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/packages/router-core/src/router.ts b/packages/router-core/src/router.ts index 99953d13db..eec8aaed08 100644 --- a/packages/router-core/src/router.ts +++ b/packages/router-core/src/router.ts @@ -37,6 +37,7 @@ import { FullSearchSchema, RouteById, RoutePaths, + RouteIds, } from './routeInfo' import { defaultParseSearch, defaultStringifySearch } from './searchParams' import { @@ -108,13 +109,13 @@ export type HydrationCtx = { export interface RouteMatch< TRouteTree extends AnyRoute = AnyRoute, - TRoute extends AnyRoute = AnyRoute, + TRouteId extends RouteIds = ParseRoute['id'], > { id: string key?: string - routeId: string + routeId: TRouteId pathname: string - params: TRoute['types']['allParams'] + params: RouteById['types']['allParams'] status: 'pending' | 'success' | 'error' isFetching: boolean invalid: boolean @@ -124,18 +125,19 @@ export interface RouteMatch< updatedAt: number invalidAt: number preloadInvalidAt: number - loaderData: TRoute['types']['loader'] + loaderData: RouteById['types']['loader'] loadPromise?: Promise __resolveLoadPromise?: () => void - routeContext: TRoute['types']['routeContext'] - context: TRoute['types']['context'] - routeSearch: TRoute['types']['searchSchema'] - search: FullSearchSchema & TRoute['types']['fullSearchSchema'] + routeContext: RouteById['types']['routeContext'] + context: RouteById['types']['context'] + routeSearch: RouteById['types']['searchSchema'] + search: FullSearchSchema & + RouteById['types']['fullSearchSchema'] fetchedAt: number abortController: AbortController } -export type AnyRouteMatch = RouteMatch +export type AnyRouteMatch = RouteMatch export type RouterContextOptions = AnyContext extends TRouteTree['types']['routerContext'] @@ -196,11 +198,11 @@ export interface RouterState< > { status: 'idle' | 'pending' isFetching: boolean - matchesById: Record>> + matchesById: Record> matchIds: string[] pendingMatchIds: string[] - matches: RouteMatch>[] - pendingMatches: RouteMatch>[] + matches: RouteMatch[] + pendingMatches: RouteMatch[] location: ParsedLocation> resolvedLocation: ParsedLocation> lastUpdated: number @@ -607,12 +609,9 @@ export class Router< } #mergeMatches = ( - prevMatchesById: Record< - string, - RouteMatch> - >, + prevMatchesById: Record>, nextMatches: AnyRouteMatch[], - ): Record>> => { + ): Record> => { const nextMatchesById: any = { ...prevMatchesById, } @@ -701,7 +700,7 @@ export class Router< pathname: string, locationSearch: AnySearchSchema, opts?: { throwOnError?: boolean; debug?: boolean }, - ): RouteMatch>[] => { + ): RouteMatch[] => { let routeParams: AnyPathParams = {} let foundRoute = this.flatRoutes.find((route) => { @@ -1715,17 +1714,13 @@ export class Router< return this.latestLoadPromise } - getRouteMatch = ( - id: string, - ): undefined | RouteMatch => { + getRouteMatch = (id: string): undefined | RouteMatch => { return this.state.matchesById[id] } setRouteMatch = ( id: string, - updater: ( - prev: RouteMatch, - ) => RouteMatch, + updater: (prev: RouteMatch) => RouteMatch, ) => { this.__store.setState((prev) => { if (!prev.matchesById[id]) {