From 4c4c347e9453358cde09266c419fcedbef92a50f Mon Sep 17 00:00:00 2001 From: webfansplz <308241863@qq.com> Date: Fri, 6 Aug 2021 21:59:18 +0800 Subject: [PATCH] refactor(types): improve of type assertions (#1064) Co-authored-by: webfansplz <> Co-authored-by: Eduardo San Martin Morote --- src/devtools.ts | 2 +- src/history/html5.ts | 4 ++-- src/history/memory.ts | 2 +- src/location.ts | 8 +++---- src/matcher/index.ts | 37 +++++++++++++++++---------------- src/matcher/pathParserRanker.ts | 2 +- src/router.ts | 2 +- src/utils/index.ts | 2 +- 8 files changed, 30 insertions(+), 29 deletions(-) diff --git a/src/devtools.ts b/src/devtools.ts index 503e19df6..002a99133 100644 --- a/src/devtools.ts +++ b/src/devtools.ts @@ -533,7 +533,7 @@ function omit(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] } diff --git a/src/history/html5.ts b/src/history/html5.ts index 6dc3daa7c..017edef1e 100644 --- a/src/history/html5.ts +++ b/src/history/html5.ts @@ -177,10 +177,10 @@ function useHistoryStateNavigation(base: string) { const { history, location } = window // private variables - let currentLocation: ValueContainer = { + const currentLocation: ValueContainer = { value: createCurrentLocation(base, location), } - let historyState: ValueContainer = { value: history.state } + const historyState: ValueContainer = { value: history.state } // build current history entry as this is a fresh navigation if (!historyState.value) { changeLocation( diff --git a/src/history/memory.ts b/src/history/memory.ts index 23648ee0e..8a22eb0ee 100644 --- a/src/history/memory.ts +++ b/src/history/memory.ts @@ -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) } } diff --git a/src/location.ts b/src/location.ts index 1b64042e0..70cfbdc84 100644 --- a/src/location.ts +++ b/src/location.ts @@ -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 } diff --git a/src/matcher/index.ts b/src/matcher/index.ts index 50ff06476..3f1aea8d5 100644 --- a/src/matcher/index.ts +++ b/src/matcher/index.ts @@ -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 { const propsObject = {} as Record // 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).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(defaults: T, partialOptions: Partial): 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}".` diff --git a/src/matcher/pathParserRanker.ts b/src/matcher/pathParserRanker.ts index 6c5d0fdb1..67ad7b537 100644 --- a/src/matcher/pathParserRanker.ts +++ b/src/matcher/pathParserRanker.ts @@ -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 = [] + const score: Array = [] // the regexp as a string let pattern = options.start ? '^' : '' // extracted keys diff --git a/src/router.ts b/src/router.ts index f32fb0e27..28b858037 100644 --- a/src/router.ts +++ b/src/router.ts @@ -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)) diff --git a/src/utils/index.ts b/src/utils/index.ts index 3e3b92189..59078e4ab 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -23,4 +23,4 @@ export function applyToParams( return newParams } -export let noop = () => {} +export const noop = () => {}