]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
refactor(types): improve of type assertions (#1064)
authorwebfansplz <308241863@qq.com>
Fri, 6 Aug 2021 13:59:18 +0000 (21:59 +0800)
committerGitHub <noreply@github.com>
Fri, 6 Aug 2021 13:59:18 +0000 (15:59 +0200)
Co-authored-by: webfansplz <>
Co-authored-by: Eduardo San Martin Morote <posva@users.noreply.github.com>
src/devtools.ts
src/history/html5.ts
src/history/memory.ts
src/location.ts
src/matcher/index.ts
src/matcher/pathParserRanker.ts
src/router.ts
src/utils/index.ts

index 503e19df6eefdd072c8bb2495b2c42c9b0ce0a7e..002a99133ceac5277e40f1bd9128bb2c7ccce8fc 100644 (file)
@@ -533,7 +533,7 @@ function omit<T extends object, K extends [...(keyof T)[]]>(obj: T, keys: K) {
   }
 
   for (const key in obj) {
-    if (!keys.includes(key as any)) {
+    if (!keys.includes(key)) {
       // @ts-expect-error
       ret[key] = obj[key]
     }
index 6dc3daa7ca19b644c666410a365aacdf5a8a0d7e..017edef1e3752a301c4d53528ead51a2066d652c 100644 (file)
@@ -177,10 +177,10 @@ function useHistoryStateNavigation(base: string) {
   const { history, location } = window
 
   // private variables
-  let currentLocation: ValueContainer<HistoryLocation> = {
+  const currentLocation: ValueContainer<HistoryLocation> = {
     value: createCurrentLocation(base, location),
   }
-  let historyState: ValueContainer<StateEntry> = { value: history.state }
+  const historyState: ValueContainer<StateEntry> = { value: history.state }
   // build current history entry as this is a fresh navigation
   if (!historyState.value) {
     changeLocation(
index 23648ee0e4df651797eea82f32162d4dfe3452eb..8a22eb0ee9e09149929605837de7d03eb6cbda57 100644 (file)
@@ -44,7 +44,7 @@ export function createMemoryHistory(base: string = ''): RouterHistory {
       delta,
       type: NavigationType.pop,
     }
-    for (let callback of listeners) {
+    for (const callback of listeners) {
       callback(to, from, info)
     }
   }
index 1b64042e0159edc26726dceb724317581e312f55..70cfbdc84e0d33eba6b52ee6b746274b071edc83 100644 (file)
@@ -93,7 +93,7 @@ export function stringifyURL(
   stringifyQuery: (query: LocationQueryRaw) => string,
   location: LocationPartial
 ): string {
-  let query: string = location.query ? stringifyQuery(location.query) : ''
+  const query: string = location.query ? stringifyQuery(location.query) : ''
   return location.path + (query && '?') + query + (location.hash || '')
 }
 
@@ -124,8 +124,8 @@ export function isSameRouteLocation(
   a: RouteLocation,
   b: RouteLocation
 ): boolean {
-  let aLastIndex = a.matched.length - 1
-  let bLastIndex = b.matched.length - 1
+  const aLastIndex = a.matched.length - 1
+  const bLastIndex = b.matched.length - 1
 
   return (
     aLastIndex > -1 &&
@@ -157,7 +157,7 @@ export function isSameRouteLocationParams(
 ): boolean {
   if (Object.keys(a).length !== Object.keys(b).length) return false
 
-  for (let key in a) {
+  for (const key in a) {
     if (!isSameRouteLocationParamsValue(a[key], b[key])) return false
   }
 
index 50ff0647635e4c17e7c35e27ffe47507d45a8d9b..3f1aea8d5ad467a1cbdab96f1f6f398e7ae1ec06 100644 (file)
@@ -5,6 +5,7 @@ import {
   isRouteName,
   RouteRecordName,
   _RouteRecordProps,
+  RouteRecordRedirect,
 } from '../types'
 import { createRouterError, ErrorTypes, MatcherError } from '../errors'
 import { createRouteRecordMatcher, RouteRecordMatcher } from './pathMatcher'
@@ -76,8 +77,8 @@ export function createRouterMatcher(
     originalRecord?: RouteRecordMatcher
   ) {
     // used later on to remove by name
-    let isRootAdd = !originalRecord
-    let mainNormalizedRecord = normalizeRouteRecord(record)
+    const isRootAdd = !originalRecord
+    const mainNormalizedRecord = normalizeRouteRecord(record)
     // we might be the child of an alias
     mainNormalizedRecord.aliasOf = originalRecord && originalRecord.record
     const options: PathParserOptions = mergeOptions(globalOptions, record)
@@ -112,13 +113,13 @@ export function createRouterMatcher(
     let originalMatcher: RouteRecordMatcher | undefined
 
     for (const normalizedRecord of normalizedRecords) {
-      let { path } = normalizedRecord
+      const { path } = normalizedRecord
       // Build up the path for nested routes if the child isn't an absolute
       // route. Only add the / delimiter if the child path isn't empty and if the
       // parent path doesn't have a trailing slash
       if (parent && path[0] !== '/') {
-        let parentPath = parent.record.path
-        let connectingSlash =
+        const parentPath = parent.record.path
+        const connectingSlash =
           parentPath[parentPath.length - 1] === '/' ? '' : '/'
         normalizedRecord.path =
           parent.record.path + (path && connectingSlash + path)
@@ -156,7 +157,7 @@ export function createRouterMatcher(
       }
 
       if ('children' in mainNormalizedRecord) {
-        let children = mainNormalizedRecord.children
+        const children = mainNormalizedRecord.children
         for (let i = 0; i < children.length; i++) {
           addRoute(
             children[i],
@@ -196,7 +197,7 @@ export function createRouterMatcher(
         matcher.alias.forEach(removeRoute)
       }
     } else {
-      let index = matchers.indexOf(matcherRef)
+      const index = matchers.indexOf(matcherRef)
       if (index > -1) {
         matchers.splice(index, 1)
         if (matcherRef.record.name) matcherMap.delete(matcherRef.record.name)
@@ -322,9 +323,9 @@ function paramsFromLocation(
   params: MatcherLocation['params'],
   keys: string[]
 ): MatcherLocation['params'] {
-  let newParams = {} as MatcherLocation['params']
+  const newParams = {} as MatcherLocation['params']
 
-  for (let key of keys) {
+  for (const key of keys) {
     if (key in params) newParams[key] = params[key]
   }
 
@@ -370,13 +371,14 @@ function normalizeRecordProps(
 ): Record<string, _RouteRecordProps> {
   const propsObject = {} as Record<string, _RouteRecordProps>
   // props does not exist on redirect records but we can set false directly
-  const props = (record as any).props || false
+  const props =
+    (record as Exclude<RouteRecordRaw, RouteRecordRedirect>).props || false
   if ('component' in record) {
     propsObject.default = props
   } else {
     // NOTE: we could also allow a function to be applied to every component.
     // Would need user feedback for use cases
-    for (let name in record.components)
+    for (const name in record.components)
       propsObject[name] = typeof props === 'boolean' ? props : props[name]
   }
 
@@ -409,10 +411,9 @@ function mergeMetaFields(matched: MatcherLocation['matched']) {
 }
 
 function mergeOptions<T>(defaults: T, partialOptions: Partial<T>): T {
-  let options = {} as T
-  for (let key in defaults) {
-    options[key] =
-      key in partialOptions ? partialOptions[key] : (defaults[key] as any)
+  const options = {} as T
+  for (const key in defaults) {
+    options[key] = key in partialOptions ? partialOptions[key]! : defaults[key]
   }
 
   return options
@@ -435,13 +436,13 @@ function isSameParam(a: ParamKey, b: ParamKey): boolean {
  * @param b - alias record
  */
 function checkSameParams(a: RouteRecordMatcher, b: RouteRecordMatcher) {
-  for (let key of a.keys) {
+  for (const key of a.keys) {
     if (!key.optional && !b.keys.find(isSameParam.bind(null, key)))
       return warn(
         `Alias "${b.record.path}" and the original record: "${a.record.path}" should have the exact same param named "${key.name}"`
       )
   }
-  for (let key of b.keys) {
+  for (const key of b.keys) {
     if (!key.optional && !a.keys.find(isSameParam.bind(null, key)))
       return warn(
         `Alias "${b.record.path}" and the original record: "${a.record.path}" should have the exact same param named "${key.name}"`
@@ -453,7 +454,7 @@ function checkMissingParamsInAbsolutePath(
   record: RouteRecordMatcher,
   parent: RouteRecordMatcher
 ) {
-  for (let key of parent.keys) {
+  for (const key of parent.keys) {
     if (!record.keys.find(isSameParam.bind(null, key)))
       return warn(
         `Absolute path "${record.record.path}" should have the exact same param named "${key.name}" as its parent "${parent.record.path}".`
index 6c5d0fdb12728705238dbd9a654f753a75a2a592..67ad7b53794ab4b4626e6e8bd965486f8d76afbc 100644 (file)
@@ -122,7 +122,7 @@ export function tokensToParser(
   const options = assign({}, BASE_PATH_PARSER_OPTIONS, extraOptions)
 
   // the amount of scores is the same as the length of segments except for the root segment "/"
-  let score: Array<number[]> = []
+  const score: Array<number[]> = []
   // the regexp as a string
   let pattern = options.start ? '^' : ''
   // extracted keys
index f32fb0e27729ffc64662f2ecf6a7923c70227aa7..28b8580373d0ea072471becdcb4b63d03c74c13f 100644 (file)
@@ -827,7 +827,7 @@ export function createRouter(options: RouterOptions): Router {
           guards = []
           for (const record of to.matched) {
             // do not trigger beforeEnter on reused views
-            if (record.beforeEnter && !from.matched.includes(record as any)) {
+            if (record.beforeEnter && !from.matched.includes(record)) {
               if (Array.isArray(record.beforeEnter)) {
                 for (const beforeEnter of record.beforeEnter)
                   guards.push(guardToPromiseFn(beforeEnter, to, from))
index 3e3b9218987d7deb87200828c0c8a00aecc40008..59078e4ab56957f006cbcf1b3d46dc869edcdc10 100644 (file)
@@ -23,4 +23,4 @@ export function applyToParams(
   return newParams
 }
 
-export let noop = () => {}
+export const noop = () => {}