]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
refactor: use RouteRecordNameGeneric
authorEduardo San Martin Morote <posva13@gmail.com>
Thu, 13 Jun 2024 08:50:34 +0000 (10:50 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Thu, 13 Jun 2024 08:50:34 +0000 (10:50 +0200)
It seems to still have value as it work in app code...

packages/router/src/index.ts
packages/router/src/matcher/index.ts
packages/router/src/router.ts
packages/router/src/typed-routes/route-location.ts
packages/router/src/typed-routes/route-records.ts
packages/router/src/types/index.ts
packages/router/src/types/typeGuards.ts

index c40e2d2cda6e5e0f85cd0cc5b54343cce0f6b36a..2a62ad15670c4d4f21d863a81b4cf2cace137422 100644 (file)
@@ -114,6 +114,7 @@ export type {
 
   // route records
   RouteRecordInfo,
+  RouteRecordNameGeneric,
   RouteRecordName,
   _RouteRecordProps,
   RouteRecordRedirectOption,
index c4ad8f55b242a93b87962ae1661603a08964473e..fc88dacadef0d0ac4590ce821cbb7ff6015c8e81 100644 (file)
@@ -18,7 +18,7 @@ import { comparePathParserScore } from './pathParserRanker'
 
 import { warn } from '../warning'
 import { assign, noop } from '../utils'
-import type { RouteRecordName, _RouteRecordProps } from '../typed-routes'
+import type { RouteRecordNameGeneric, _RouteRecordProps } from '../typed-routes'
 
 /**
  * Internal RouterMatcher
@@ -29,11 +29,11 @@ export interface RouterMatcher {
   addRoute: (record: RouteRecordRaw, parent?: RouteRecordMatcher) => () => void
 
   removeRoute(matcher: RouteRecordMatcher): void
-  removeRoute(name: NonNullable<RouteRecordName>): void
+  removeRoute(name: NonNullable<RouteRecordNameGeneric>): void
 
   getRoutes: () => RouteRecordMatcher[]
   getRecordMatcher: (
-    name: NonNullable<RouteRecordName>
+    name: NonNullable<RouteRecordNameGeneric>
   ) => RouteRecordMatcher | undefined
 
   /**
@@ -61,13 +61,16 @@ export function createRouterMatcher(
 ): RouterMatcher {
   // normalized ordered array of matchers
   const matchers: RouteRecordMatcher[] = []
-  const matcherMap = new Map<RouteRecordName, RouteRecordMatcher>()
+  const matcherMap = new Map<
+    NonNullable<RouteRecordNameGeneric>,
+    RouteRecordMatcher
+  >()
   globalOptions = mergeOptions(
     { strict: false, end: true, sensitive: false } as PathParserOptions,
     globalOptions
   )
 
-  function getRecordMatcher(name: RouteRecordName) {
+  function getRecordMatcher(name: NonNullable<RouteRecordNameGeneric>) {
     return matcherMap.get(name)
   }
 
@@ -195,7 +198,7 @@ export function createRouterMatcher(
   }
 
   function removeRoute(
-    matcherRef: NonNullable<RouteRecordName> | RouteRecordMatcher
+    matcherRef: NonNullable<RouteRecordNameGeneric> | RouteRecordMatcher
   ) {
     if (isRouteName(matcherRef)) {
       const matcher = matcherMap.get(matcherRef)
index db12647fee01492ec090d17e72e4a895e01869d1..20df891ab882005a43d3d832c3e7b27eeb450420 100644 (file)
@@ -18,7 +18,7 @@ import type {
   RouteLocationAsRelative,
   RouteLocationAsPath,
   RouteLocationAsString,
-  RouteRecordName,
+  RouteRecordNameGeneric,
 } from './typed-routes'
 import { RouterHistory, HistoryState, NavigationType } from './history/common'
 import {
@@ -212,8 +212,8 @@ export interface Router {
    * @param route - Route Record to add
    */
   addRoute(
-    // NOTE: RouteRecordName could be `keyof RouteMap` but the point of dynamic routes is not knowing the routes at build
-    parentName: NonNullable<RouteRecordName>,
+    // NOTE: it could be `keyof RouteMap` but the point of dynamic routes is not knowing the routes at build
+    parentName: NonNullable<RouteRecordNameGeneric>,
     route: RouteRecordRaw
   ): () => void
   /**
@@ -227,13 +227,13 @@ export interface Router {
    *
    * @param name - Name of the route to remove
    */
-  removeRoute(name: NonNullable<RouteRecordName>): void
+  removeRoute(name: NonNullable<RouteRecordNameGeneric>): void
   /**
    * Checks if a route with a given name exists
    *
    * @param name - Name of the route to check
    */
-  hasRoute(name: NonNullable<RouteRecordName>): boolean
+  hasRoute(name: NonNullable<RouteRecordNameGeneric>): boolean
   /**
    * Get a full list of all the {@link RouteRecord | route records}.
    */
@@ -411,7 +411,7 @@ export function createRouter(options: RouterOptions): Router {
     applyToParams.bind(null, decode)
 
   function addRoute(
-    parentOrRoute: NonNullable<RouteRecordName> | RouteRecordRaw,
+    parentOrRoute: NonNullable<RouteRecordNameGeneric> | RouteRecordRaw,
     route?: RouteRecordRaw
   ) {
     let parent: Parameters<(typeof matcher)['addRoute']>[1] | undefined
@@ -434,7 +434,7 @@ export function createRouter(options: RouterOptions): Router {
     return matcher.addRoute(record, parent)
   }
 
-  function removeRoute(name: NonNullable<RouteRecordName>) {
+  function removeRoute(name: NonNullable<RouteRecordNameGeneric>) {
     const recordMatcher = matcher.getRecordMatcher(name)
     if (recordMatcher) {
       matcher.removeRoute(recordMatcher)
@@ -447,7 +447,7 @@ export function createRouter(options: RouterOptions): Router {
     return matcher.getRoutes().map(routeMatcher => routeMatcher.record)
   }
 
-  function hasRoute(name: NonNullable<RouteRecordName>): boolean {
+  function hasRoute(name: NonNullable<RouteRecordNameGeneric>): boolean {
     return !!matcher.getRecordMatcher(name)
   }
 
index e68f3e2b68cdb0b10e008de2457a1fe2c4516c1c..3be525760953334e523e2cd120f631a11d759406 100644 (file)
@@ -11,7 +11,7 @@ import type { _LiteralUnion } from '../types/utils'
 import type { RouteMap, RouteMapGeneric } from './route-map'
 import type { Router } from '../router'
 import type { RouteRecord, RouteRecordNormalized } from '../matcher/types'
-import type { RouteRecordName } from './route-records'
+import type { RouteRecordNameGeneric } from './route-records'
 
 /**
  * Generic version of {@link RouteLocation}. It is used when no {@link RouteMap} is provided.
@@ -49,7 +49,7 @@ export type RouteLocationTypedList<
  * Generic version of {@link RouteLocationNormalized} that is used when no {@link RouteMap} is provided.
  */
 export interface RouteLocationNormalizedGeneric extends _RouteLocationBase {
-  name: RouteRecordName
+  name: RouteRecordNameGeneric
   params: RouteParamsGeneric
   /**
    * Array of {@link RouteRecordNormalized}
@@ -122,7 +122,7 @@ export type RouteLocationNormalizedLoadedTypedList<
 export interface RouteLocationAsRelativeGeneric
   extends RouteQueryAndHash,
     RouteLocationOptions {
-  name?: RouteRecordName
+  name?: RouteRecordNameGeneric
   params?: RouteParamsRawGeneric
   /**
    * A relative path to the current location. This property should be removed
index 14509864ba8b6249e4ab0ade16b650a6e1e51251..d7d9d2022e94c3a2f81aa4d16a6c4b33fd025f46 100644 (file)
@@ -3,7 +3,7 @@ import type {
   RouteLocationNormalized,
   RouteLocationRaw,
 } from './route-location'
-import type { RouteMap } from './route-map'
+import type { RouteMap, RouteMapGeneric } from './route-map'
 
 /**
  * @internal
@@ -12,12 +12,19 @@ export type RouteRecordRedirectOption =
   | RouteLocationRaw
   | ((to: RouteLocation) => RouteLocationRaw)
 
+/**
+ * Generic version of {@link RouteRecordName}.
+ */
+export type RouteRecordNameGeneric = string | symbol | undefined
+
 /**
  * Possible values for a route record **after normalization**
  *
- * 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`}
+ * 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`}
  */
-export type RouteRecordName = string | symbol | undefined
+export type RouteRecordName = RouteMapGeneric extends RouteMap
+  ? RouteRecordNameGeneric
+  : keyof RouteMap
 
 /**
  * @internal
index b9383b1631a591070633da18ff588823a7dd3426..e3069740b0d806c03ed97060c2b5d191e2730485 100644 (file)
@@ -8,7 +8,7 @@ import type {
   RouteLocation,
   RouteRecordRedirectOption,
   _RouteRecordProps,
-  RouteRecordName,
+  RouteRecordNameGeneric,
 } from '../typed-routes'
 import type { _Awaitable } from './utils'
 
@@ -67,7 +67,7 @@ export interface MatcherLocationAsPath {
  * @internal
  */
 export interface MatcherLocationAsName {
-  name: RouteRecordName
+  name: RouteRecordNameGeneric
   // to allow checking location.path == null
   /**
    * Ignored path property since we are dealing with a relative location. Only `undefined` is allowed.
@@ -92,7 +92,7 @@ export interface MatcherLocationAsRelative {
  * @internal
  */
 export interface LocationAsRelativeRaw {
-  name?: RouteRecordName
+  name?: RouteRecordNameGeneric
   // to allow checking location.path == null
   /**
    * Ignored path property since we are dealing with a relative location. Only `undefined` is allowed.
@@ -215,7 +215,7 @@ export interface _RouteRecordBase extends PathParserOptions {
   /**
    * Name for the route record. Must be unique.
    */
-  name?: RouteRecordName
+  name?: RouteRecordNameGeneric
 
   /**
    * Before Enter guard specific to this record. Note `beforeEnter` has no
@@ -376,7 +376,7 @@ export interface MatcherLocation {
   /**
    * Name of the matched record
    */
-  name: RouteRecordName | null | undefined
+  name: RouteRecordNameGeneric | null | undefined
 
   /**
    * Percentage encoded pathname section of the URL.
index 7eac281d3c13748b3179618b0695bc821eaac159..ba30bd9b635eed703dc645baf064639e98cd31c9 100644 (file)
@@ -1,9 +1,9 @@
-import type { RouteLocationRaw, RouteRecordName } from '../typed-routes'
+import type { RouteLocationRaw, RouteRecordNameGeneric } from '../typed-routes'
 
 export function isRouteLocation(route: any): route is RouteLocationRaw {
   return typeof route === 'string' || (route && typeof route === 'object')
 }
 
-export function isRouteName(name: any): name is RouteRecordName {
+export function isRouteName(name: any): name is RouteRecordNameGeneric {
   return typeof name === 'string' || typeof name === 'symbol'
 }