From: Eduardo San Martin Morote Date: Mon, 13 Jun 2022 10:32:27 +0000 (+0200) Subject: perf(types): reduce tuple amount for string paths X-Git-Tag: v4.1.0~26 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=be942c838684613393c726b7eccfa115617c7fad;p=thirdparty%2Fvuejs%2Frouter.git perf(types): reduce tuple amount for string paths --- diff --git a/packages/playground/package.json b/packages/playground/package.json index ca91d85b..f04e2261 100644 --- a/packages/playground/package.json +++ b/packages/playground/package.json @@ -4,7 +4,7 @@ "version": "0.0.0", "scripts": { "dev": "vite", - "build": "vite build", + "build": "vue-tsc --noEmit && vite build", "preview": "vite preview --port 4173" }, "dependencies": { diff --git a/packages/playground/src/router.ts b/packages/playground/src/router.ts index 1fa32c5b..c4c1b610 100644 --- a/packages/playground/src/router.ts +++ b/packages/playground/src/router.ts @@ -146,14 +146,14 @@ export const router = createRouter({ component: Nested, end: false, strict: true, - beforeEnter(to, from, next) { + beforeEnter(to) { if (!removeRoute) { removeRoute = router.addRoute('dynamic', { path: 'child', component: Dynamic, }) - next(to.fullPath) - } else next() + return to.fullPath + } }, }, diff --git a/packages/playground/tsconfig.json b/packages/playground/tsconfig.json index b30e9f0d..9b914b7f 100644 --- a/packages/playground/tsconfig.json +++ b/packages/playground/tsconfig.json @@ -1,8 +1,9 @@ { "extends": "@vue/tsconfig/tsconfig.web.json", - "include": ["env.d.ts", "src/**/*", "src/**/*.vue"], + "include": ["env.d.ts", "src/**/*.ts", "src/**/*.vue"], "exclude": ["**/node_modules", "**/.*/"], "compilerOptions": { + "allowJs": false, "baseUrl": ".", "paths": { "@/*": ["./src/*"] diff --git a/packages/router/src/types/index.ts b/packages/router/src/types/index.ts index 498fc399..291c0b90 100644 --- a/packages/router/src/types/index.ts +++ b/packages/router/src/types/index.ts @@ -9,6 +9,7 @@ import { RouteNamedMapGeneric, RouteStaticPathMapGeneric, } from './named' +import { LiteralUnion } from './utils' export type Lazy = () => Promise export type Override = Pick> & U @@ -132,9 +133,12 @@ export type RouteLocationString< RouteMap extends RouteStaticPathMapGeneric = RouteStaticPathMapGeneric > = RouteStaticPathMapGeneric extends RouteMap ? string - : { - [K in keyof RouteMap]: RouteMap[K]['fullPath'] - }[keyof RouteMap] + : LiteralUnion< + { + [K in keyof RouteMap]: RouteMap[K] + }[keyof RouteMap], + string + > /** * Route Location that can infer the necessary params based on the name. @@ -162,11 +166,14 @@ export type RouteLocationPathRaw< > = RouteStaticPathMapGeneric extends RouteMap ? // allows assigning a RouteLocationRaw to RouteLocationPat RouteQueryAndHash & LocationAsPath & RouteLocationOptions - : { - [K in Extract]: RouteQueryAndHash & - LocationAsPath & - RouteLocationOptions - }[Extract] + : RouteQueryAndHash & + RouteLocationOptions & + LocationAsPath< + LiteralUnion< + { [K in keyof RouteMap]: RouteMap[K] }[keyof RouteMap], + string + > + > export interface RouteLocationMatched extends RouteRecordNormalized { // components cannot be Lazy diff --git a/packages/router/src/types/named.ts b/packages/router/src/types/named.ts index a662969f..285c554f 100644 --- a/packages/router/src/types/named.ts +++ b/packages/router/src/types/named.ts @@ -32,7 +32,6 @@ export type RouteNamedMap< [N in Name]: { // name: N params: ParamsFromPath<_JoinPath> - // TODO: ParamsRawFromPath paramsRaw: ParamsRawFromPath<_JoinPath> path: _JoinPath } @@ -55,31 +54,6 @@ export type RouteNamedMap< // END: 1 } -/** - * Type that adds valid semi literal paths to still enable autocomplete while allowing proper paths - */ -type _PathForAutocomplete

= P extends `${string}:${string}` - ? LiteralUnion> - : P - -/** - * @internal - */ -export type _PathWithHash

= `${P}#${string}` - -/** - * @internal - */ -export type _PathWithQuery

= `${P}?${string}` - -/** - * @internal - */ -export type _FullPath

= LiteralUnion< - P, - _PathWithHash

| _PathWithQuery

-> - /** * @internal */ @@ -94,12 +68,7 @@ export type RouteStaticPathMap< infer Children > ? { - // TODO: add | ${string} for params - // TODO: add extra type to append ? and # variants - [P in Path as _JoinPath]: { - path: _PathForAutocomplete<_JoinPath> - fullPath: _FullPath<_PathForAutocomplete<_JoinPath>> - } + [P in Path as _JoinPath]: _JoinPath } & (Children extends Readonly // Recurse children ? RouteStaticPathMap> : { @@ -141,10 +110,7 @@ export type RouteNamedMapGeneric = Record * * @internal */ -export type RouteStaticPathMapGeneric = Record< - string, - { path: string; fullPath: string } -> +export type RouteStaticPathMapGeneric = Record /** * Relevant information about a named route record to deduce its params. diff --git a/packages/router/test-dts/namedRoutes.test-d.ts b/packages/router/test-dts/namedRoutes.test-d.ts index 76b22a36..00bf9362 100644 --- a/packages/router/test-dts/namedRoutes.test-d.ts +++ b/packages/router/test-dts/namedRoutes.test-d.ts @@ -92,21 +92,20 @@ for (const method of methods) { // paths r2[method]({ path: '/nested' }) + r2[method]({ path: '/nested/:a/b' }) + // with an actual param r2[method]({ path: '/nested/a/b' }) - // @ts-expect-error + // NOTE: we actually accept any string because of perf bottlenecks due to tuples r2[method]({ path: '' }) - // @ts-expect-error r2[method]({ path: '/nope' }) - // @ts-expect-error r2[method]({ path: '/no-name?query' }) - // @ts-expect-error r2[method]({ path: '/no-name#hash' }) r2[method]('/nested') r2[method]('/nested/a/b') - // @ts-expect-error + + // NOTE: same as above r2[method]('') - // @ts-expect-error r2[method]('/nope') r2[method]('/no-name?query') r2[method]('/no-name#hash')