]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
perf(types): reduce tuple amount for string paths
authorEduardo San Martin Morote <posva13@gmail.com>
Mon, 13 Jun 2022 10:32:27 +0000 (12:32 +0200)
committerEduardo San Martin Morote <posva@users.noreply.github.com>
Thu, 30 Jun 2022 07:59:00 +0000 (09:59 +0200)
packages/playground/package.json
packages/playground/src/router.ts
packages/playground/tsconfig.json
packages/router/src/types/index.ts
packages/router/src/types/named.ts
packages/router/test-dts/namedRoutes.test-d.ts

index ca91d85b75c85a771de22cf37a9e285a8aae3058..f04e2261d5c404caa0d83915f718fc1e4388dca5 100644 (file)
@@ -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": {
index 1fa32c5b64ff345a33ec8173e88dbead9395c784..c4c1b6104e4550f1947e46f75f287c116e9e13ab 100644 (file)
@@ -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
+        }
       },
     },
 
index b30e9f0d4b4c0941eb3f79eec86438165a02964c..9b914b7f248084df3f6bc31062ba22e1b087f9b0 100644 (file)
@@ -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/*"]
index 498fc399cf2c44ea6cc071a54886a72c169e483f..291c0b90283f4f843a63574b112231a59366b58a 100644 (file)
@@ -9,6 +9,7 @@ import {
   RouteNamedMapGeneric,
   RouteStaticPathMapGeneric,
 } from './named'
+import { LiteralUnion } from './utils'
 
 export type Lazy<T> = () => Promise<T>
 export type Override<T, U> = Pick<T, Exclude<keyof T, keyof U>> & 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<keyof RouteMap, string>]: RouteQueryAndHash &
-        LocationAsPath<RouteMap[K]['path']> &
-        RouteLocationOptions
-    }[Extract<keyof RouteMap, string>]
+  : RouteQueryAndHash &
+      RouteLocationOptions &
+      LocationAsPath<
+        LiteralUnion<
+          { [K in keyof RouteMap]: RouteMap[K] }[keyof RouteMap],
+          string
+        >
+      >
 
 export interface RouteLocationMatched extends RouteRecordNormalized {
   // components cannot be Lazy<RouteComponent>
index a662969ffff74ea05982be97559687cc8451c6f3..285c554ff7f8b3ea4ff971f7b2b0c61abd764dbf 100644 (file)
@@ -32,7 +32,6 @@ export type RouteNamedMap<
                 [N in Name]: {
                   // name: N
                   params: ParamsFromPath<_JoinPath<Prefix, Path>>
-                  // TODO: ParamsRawFromPath
                   paramsRaw: ParamsRawFromPath<_JoinPath<Prefix, Path>>
                   path: _JoinPath<Prefix, Path>
                 }
@@ -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> = P extends `${string}:${string}`
-  ? LiteralUnion<P, PathFromParams<P>>
-  : P
-
-/**
- * @internal
- */
-export type _PathWithHash<P extends string> = `${P}#${string}`
-
-/**
- * @internal
- */
-export type _PathWithQuery<P extends string> = `${P}?${string}`
-
-/**
- * @internal
- */
-export type _FullPath<P extends string> = LiteralUnion<
-  P,
-  _PathWithHash<P> | _PathWithQuery<P>
->
-
 /**
  * @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<Prefix, Path>]: {
-              path: _PathForAutocomplete<_JoinPath<Prefix, Path>>
-              fullPath: _FullPath<_PathForAutocomplete<_JoinPath<Prefix, Path>>>
-            }
+            [P in Path as _JoinPath<Prefix, Path>]: _JoinPath<Prefix, Path>
           } & (Children extends Readonly<RouteRecordRaw[]> // Recurse children
             ? RouteStaticPathMap<Children, _JoinPath<Prefix, Path>>
             : {
@@ -141,10 +110,7 @@ export type RouteNamedMapGeneric = Record<RouteRecordName, RouteNamedInfo>
  *
  * @internal
  */
-export type RouteStaticPathMapGeneric = Record<
-  string,
-  { path: string; fullPath: string }
->
+export type RouteStaticPathMapGeneric = Record<string, string>
 
 /**
  * Relevant information about a named route record to deduce its params.
index 76b22a36c3d2e08ea57282546794c4d3b67acdd2..00bf936274ee85e8f4429fbc917302abe3218a6b 100644 (file)
@@ -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')