Skip to content

Commit 2510170

Browse files
committed
refactor: use RouteRecordNameGeneric
It seems to still have value as it work in app code...
1 parent 3675b05 commit 2510170

File tree

7 files changed

+38
-27
lines changed

7 files changed

+38
-27
lines changed

packages/router/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ export type {
114114

115115
// route records
116116
RouteRecordInfo,
117+
RouteRecordNameGeneric,
117118
RouteRecordName,
118119
_RouteRecordProps,
119120
RouteRecordRedirectOption,

packages/router/src/matcher/index.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { comparePathParserScore } from './pathParserRanker'
1818

1919
import { warn } from '../warning'
2020
import { assign, noop } from '../utils'
21-
import type { RouteRecordName, _RouteRecordProps } from '../typed-routes'
21+
import type { RouteRecordNameGeneric, _RouteRecordProps } from '../typed-routes'
2222

2323
/**
2424
* Internal RouterMatcher
@@ -29,11 +29,11 @@ export interface RouterMatcher {
2929
addRoute: (record: RouteRecordRaw, parent?: RouteRecordMatcher) => () => void
3030

3131
removeRoute(matcher: RouteRecordMatcher): void
32-
removeRoute(name: NonNullable<RouteRecordName>): void
32+
removeRoute(name: NonNullable<RouteRecordNameGeneric>): void
3333

3434
getRoutes: () => RouteRecordMatcher[]
3535
getRecordMatcher: (
36-
name: NonNullable<RouteRecordName>
36+
name: NonNullable<RouteRecordNameGeneric>
3737
) => RouteRecordMatcher | undefined
3838

3939
/**
@@ -61,13 +61,16 @@ export function createRouterMatcher(
6161
): RouterMatcher {
6262
// normalized ordered array of matchers
6363
const matchers: RouteRecordMatcher[] = []
64-
const matcherMap = new Map<RouteRecordName, RouteRecordMatcher>()
64+
const matcherMap = new Map<
65+
NonNullable<RouteRecordNameGeneric>,
66+
RouteRecordMatcher
67+
>()
6568
globalOptions = mergeOptions(
6669
{ strict: false, end: true, sensitive: false } as PathParserOptions,
6770
globalOptions
6871
)
6972

70-
function getRecordMatcher(name: RouteRecordName) {
73+
function getRecordMatcher(name: NonNullable<RouteRecordNameGeneric>) {
7174
return matcherMap.get(name)
7275
}
7376

@@ -195,7 +198,7 @@ export function createRouterMatcher(
195198
}
196199

197200
function removeRoute(
198-
matcherRef: NonNullable<RouteRecordName> | RouteRecordMatcher
201+
matcherRef: NonNullable<RouteRecordNameGeneric> | RouteRecordMatcher
199202
) {
200203
if (isRouteName(matcherRef)) {
201204
const matcher = matcherMap.get(matcherRef)

packages/router/src/router.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import type {
1818
RouteLocationAsRelative,
1919
RouteLocationAsPath,
2020
RouteLocationAsString,
21-
RouteRecordName,
21+
RouteRecordNameGeneric,
2222
} from './typed-routes'
2323
import { RouterHistory, HistoryState, NavigationType } from './history/common'
2424
import {
@@ -212,8 +212,8 @@ export interface Router {
212212
* @param route - Route Record to add
213213
*/
214214
addRoute(
215-
// NOTE: RouteRecordName could be `keyof RouteMap` but the point of dynamic routes is not knowing the routes at build
216-
parentName: NonNullable<RouteRecordName>,
215+
// NOTE: it could be `keyof RouteMap` but the point of dynamic routes is not knowing the routes at build
216+
parentName: NonNullable<RouteRecordNameGeneric>,
217217
route: RouteRecordRaw
218218
): () => void
219219
/**
@@ -227,13 +227,13 @@ export interface Router {
227227
*
228228
* @param name - Name of the route to remove
229229
*/
230-
removeRoute(name: NonNullable<RouteRecordName>): void
230+
removeRoute(name: NonNullable<RouteRecordNameGeneric>): void
231231
/**
232232
* Checks if a route with a given name exists
233233
*
234234
* @param name - Name of the route to check
235235
*/
236-
hasRoute(name: NonNullable<RouteRecordName>): boolean
236+
hasRoute(name: NonNullable<RouteRecordNameGeneric>): boolean
237237
/**
238238
* Get a full list of all the {@link RouteRecord | route records}.
239239
*/
@@ -411,7 +411,7 @@ export function createRouter(options: RouterOptions): Router {
411411
applyToParams.bind(null, decode)
412412

413413
function addRoute(
414-
parentOrRoute: NonNullable<RouteRecordName> | RouteRecordRaw,
414+
parentOrRoute: NonNullable<RouteRecordNameGeneric> | RouteRecordRaw,
415415
route?: RouteRecordRaw
416416
) {
417417
let parent: Parameters<(typeof matcher)['addRoute']>[1] | undefined
@@ -434,7 +434,7 @@ export function createRouter(options: RouterOptions): Router {
434434
return matcher.addRoute(record, parent)
435435
}
436436

437-
function removeRoute(name: NonNullable<RouteRecordName>) {
437+
function removeRoute(name: NonNullable<RouteRecordNameGeneric>) {
438438
const recordMatcher = matcher.getRecordMatcher(name)
439439
if (recordMatcher) {
440440
matcher.removeRoute(recordMatcher)
@@ -447,7 +447,7 @@ export function createRouter(options: RouterOptions): Router {
447447
return matcher.getRoutes().map(routeMatcher => routeMatcher.record)
448448
}
449449

450-
function hasRoute(name: NonNullable<RouteRecordName>): boolean {
450+
function hasRoute(name: NonNullable<RouteRecordNameGeneric>): boolean {
451451
return !!matcher.getRecordMatcher(name)
452452
}
453453

packages/router/src/typed-routes/route-location.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type { _LiteralUnion } from '../types/utils'
1111
import type { RouteMap, RouteMapGeneric } from './route-map'
1212
import type { Router } from '../router'
1313
import type { RouteRecord, RouteRecordNormalized } from '../matcher/types'
14-
import type { RouteRecordName } from './route-records'
14+
import type { RouteRecordNameGeneric } from './route-records'
1515

1616
/**
1717
* Generic version of {@link RouteLocation}. It is used when no {@link RouteMap} is provided.
@@ -49,7 +49,7 @@ export type RouteLocationTypedList<
4949
* Generic version of {@link RouteLocationNormalized} that is used when no {@link RouteMap} is provided.
5050
*/
5151
export interface RouteLocationNormalizedGeneric extends _RouteLocationBase {
52-
name: RouteRecordName
52+
name: RouteRecordNameGeneric
5353
params: RouteParamsGeneric
5454
/**
5555
* Array of {@link RouteRecordNormalized}
@@ -122,7 +122,7 @@ export type RouteLocationNormalizedLoadedTypedList<
122122
export interface RouteLocationAsRelativeGeneric
123123
extends RouteQueryAndHash,
124124
RouteLocationOptions {
125-
name?: RouteRecordName
125+
name?: RouteRecordNameGeneric
126126
params?: RouteParamsRawGeneric
127127
/**
128128
* A relative path to the current location. This property should be removed

packages/router/src/typed-routes/route-records.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type {
33
RouteLocationNormalized,
44
RouteLocationRaw,
55
} from './route-location'
6-
import type { RouteMap } from './route-map'
6+
import type { RouteMap, RouteMapGeneric } from './route-map'
77

88
/**
99
* @internal
@@ -12,12 +12,19 @@ export type RouteRecordRedirectOption =
1212
| RouteLocationRaw
1313
| ((to: RouteLocation) => RouteLocationRaw)
1414

15+
/**
16+
* Generic version of {@link RouteRecordName}.
17+
*/
18+
export type RouteRecordNameGeneric = string | symbol | undefined
19+
1520
/**
1621
* Possible values for a route record **after normalization**
1722
*
18-
* NOTE: since `RouteRecordName` is a type, it evaluates too early and it's always be a generic version. If you need a typed version of all of the names of routes, use {@link RouteMap | `keyof RouteMap`}
23+
* NOTE: since `RouteRecordName` is a type, it evaluates too early and it's often the generic version {@link RouteRecordNameGeneric}. If you need a typed version of all of the names of routes, use {@link RouteMap | `keyof RouteMap`}
1924
*/
20-
export type RouteRecordName = string | symbol | undefined
25+
export type RouteRecordName = RouteMapGeneric extends RouteMap
26+
? RouteRecordNameGeneric
27+
: keyof RouteMap
2128

2229
/**
2330
* @internal

packages/router/src/types/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type {
88
RouteLocation,
99
RouteRecordRedirectOption,
1010
_RouteRecordProps,
11-
RouteRecordName,
11+
RouteRecordNameGeneric,
1212
} from '../typed-routes'
1313
import type { _Awaitable } from './utils'
1414

@@ -67,7 +67,7 @@ export interface MatcherLocationAsPath {
6767
* @internal
6868
*/
6969
export interface MatcherLocationAsName {
70-
name: RouteRecordName
70+
name: RouteRecordNameGeneric
7171
// to allow checking location.path == null
7272
/**
7373
* Ignored path property since we are dealing with a relative location. Only `undefined` is allowed.
@@ -92,7 +92,7 @@ export interface MatcherLocationAsRelative {
9292
* @internal
9393
*/
9494
export interface LocationAsRelativeRaw {
95-
name?: RouteRecordName
95+
name?: RouteRecordNameGeneric
9696
// to allow checking location.path == null
9797
/**
9898
* Ignored path property since we are dealing with a relative location. Only `undefined` is allowed.
@@ -215,7 +215,7 @@ export interface _RouteRecordBase extends PathParserOptions {
215215
/**
216216
* Name for the route record. Must be unique.
217217
*/
218-
name?: RouteRecordName
218+
name?: RouteRecordNameGeneric
219219

220220
/**
221221
* Before Enter guard specific to this record. Note `beforeEnter` has no
@@ -376,7 +376,7 @@ export interface MatcherLocation {
376376
/**
377377
* Name of the matched record
378378
*/
379-
name: RouteRecordName | null | undefined
379+
name: RouteRecordNameGeneric | null | undefined
380380

381381
/**
382382
* Percentage encoded pathname section of the URL.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import type { RouteLocationRaw, RouteRecordName } from '../typed-routes'
1+
import type { RouteLocationRaw, RouteRecordNameGeneric } from '../typed-routes'
22

33
export function isRouteLocation(route: any): route is RouteLocationRaw {
44
return typeof route === 'string' || (route && typeof route === 'object')
55
}
66

7-
export function isRouteName(name: any): name is RouteRecordName {
7+
export function isRouteName(name: any): name is RouteRecordNameGeneric {
88
return typeof name === 'string' || typeof name === 'symbol'
99
}

0 commit comments

Comments
 (0)