Skip to content

Commit 06aefa8

Browse files
committed
refactor: revert the complex types for path parsing
Achieve it with a build plugin instead; https://github.com/posva/unplugin-vue-router Types were too slow due to their recursive nature and relying on tuples too much. A build-time type generation is not only reliable and fast but also enable creating other patterns.
1 parent 411bc3d commit 06aefa8

File tree

13 files changed

+38
-882
lines changed

13 files changed

+38
-882
lines changed

packages/router/src/RouterLink.ts

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,36 +31,26 @@ import {
3131
VueUseOptions,
3232
RouteLocation,
3333
RouteLocationNormalized,
34-
RouteLocationPathRaw,
35-
RouteLocationString,
36-
RouteLocationNamedRaw,
3734
} from './types'
3835
import { isSameRouteLocationParams, isSameRouteRecord } from './___location'
3936
import { routerKey, routeLocationKey } from './injectionSymbols'
4037
import { RouteRecord } from './matcher/types'
4138
import { NavigationFailure } from './errors'
4239
import { isArray, isBrowser, noop } from './utils'
43-
import type { Router } from './router'
44-
import type { RouteNamedMap, RouteStaticPathMap } from './types/named'
45-
import type { RouterTyped } from './typedRouter'
4640

47-
export interface RouterLinkOptions<
48-
Routes extends RouteLocationRaw = RouteLocationRaw
49-
> {
41+
export interface RouterLinkOptions {
5042
/**
5143
* Route Location the link should navigate to when clicked on.
5244
*/
53-
to: Routes
45+
to: RouteLocationRaw
5446
/**
5547
* Calls `router.replace` instead of `router.push`.
5648
*/
5749
replace?: boolean
5850
// TODO: refactor using extra options allowed in router.push. Needs RFC
5951
}
6052

61-
export interface RouterLinkProps<
62-
Routes extends RouteLocationRaw = RouteLocationRaw
63-
> extends RouterLinkOptions<Routes> {
53+
export interface RouterLinkProps extends RouterLinkOptions {
6454
/**
6555
* Whether RouterLink should not wrap its content in an `a` tag. Useful when
6656
* using `v-slot` to create a custom RouterLink
@@ -261,22 +251,20 @@ export const RouterLinkImpl = /*#__PURE__*/ defineComponent({
261251
/**
262252
* Component to render a link that triggers a navigation on click.
263253
*/
264-
export const RouterLink: RouterLinkTyped = RouterLinkImpl as any
254+
export const RouterLink: _RouterLinkI = RouterLinkImpl as any
265255

266256
/**
267257
* Typed version of the `RouterLink` component. Its generic defaults to the typed router so it can be inferred
268258
* automatically for JSX.
259+
*
260+
* @internal
269261
*/
270-
export interface RouterLinkTyped<R extends Router = RouterTyped> {
262+
export interface _RouterLinkI {
271263
new (): {
272264
$props: AllowedComponentProps &
273265
ComponentCustomProps &
274266
VNodeProps &
275-
RouterLinkProps<
276-
| RouteLocationNamedRaw<RouteNamedMap<R['options']['routes']>>
277-
| RouteLocationString<RouteStaticPathMap<R['options']['routes']>>
278-
| RouteLocationPathRaw<RouteStaticPathMap<R['options']['routes']>>
279-
>
267+
RouterLinkProps
280268

281269
$slots: {
282270
default: (arg: UnwrapRef<ReturnType<typeof useLink>>) => VNode[]

packages/router/src/globalExtensions.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import type {
44
RouteLocationNormalizedLoaded,
55
} from './types'
66
import { RouterView } from './RouterView'
7-
import type { RouterLinkTyped } from './RouterLink'
8-
import type { RouterTyped } from './typedRouter'
7+
import { RouterLink } from './RouterLink'
8+
import type { Router } from './router'
99

1010
declare module '@vue/runtime-core' {
1111
export interface ComponentCustomOptions {
@@ -55,11 +55,11 @@ declare module '@vue/runtime-core' {
5555
/**
5656
* {@link Router} instance used by the application.
5757
*/
58-
$router: RouterTyped
58+
$router: Router
5959
}
6060

6161
export interface GlobalComponents {
6262
RouterView: typeof RouterView
63-
RouterLink: RouterLinkTyped<RouterTyped>
63+
RouterLink: typeof RouterLink
6464
}
6565
}

packages/router/src/index.ts

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ export type {
4646
RouteParamValueRaw,
4747
RouteLocationNamedRaw,
4848
RouteLocationPathRaw,
49-
RouteLocationString,
5049
RouteLocationMatched,
5150
RouteLocationOptions,
5251
RouteRecordRedirectOption,
@@ -62,26 +61,6 @@ export type {
6261
NavigationGuardWithThis,
6362
NavigationHookAfter,
6463
} from './types'
65-
export type {
66-
ParamsFromPath,
67-
ParamsRawFromPath,
68-
_StripRegex,
69-
_RemoveUntilClosingPar,
70-
_ExtractParamsOfPath,
71-
_ParamExtractResult,
72-
_ExtractModifier,
73-
_ModifierExtracTResult,
74-
_JoinPath,
75-
_ParamDelimiter,
76-
_ParamModifier,
77-
} from './types/paths'
78-
export type {
79-
RouteNamedMap,
80-
RouteStaticPathMap,
81-
RouteNamedInfo,
82-
_RouteRecordNamedBaseInfo,
83-
} from './types/named'
84-
export type { Config, RouterTyped } from './typedRouter'
8564

8665
export { createRouter } from './router'
8766
export type { Router, RouterOptions, RouterScrollBehavior } from './router'
@@ -100,9 +79,9 @@ export {
10079
} from './navigationGuards'
10180
export { RouterLink, useLink } from './RouterLink'
10281
export type {
82+
_RouterLinkI,
10383
RouterLinkProps,
10484
UseLinkOptions,
105-
RouterLinkTyped,
10685
} from './RouterLink'
10786
export { RouterView } from './RouterView'
10887
export type { RouterViewProps } from './RouterView'

packages/router/src/injectionSymbols.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { InjectionKey, ComputedRef, Ref } from 'vue'
1+
import type { InjectionKey, ComputedRef, Ref } from 'vue'
22
import { RouteLocationNormalizedLoaded } from './types'
33
import { RouteRecordNormalized } from './matcher/types'
4-
import { RouterTyped } from './typedRouter'
4+
import type { Router } from './router'
55

66
/**
77
* RouteRecord being rendered by the closest ancestor Router View. Used for
@@ -30,9 +30,7 @@ export const viewDepthKey = Symbol(
3030
*
3131
* @internal
3232
*/
33-
export const routerKey = Symbol(
34-
__DEV__ ? 'router' : ''
35-
) as InjectionKey<RouterTyped>
33+
export const routerKey = Symbol(__DEV__ ? 'router' : '') as InjectionKey<Router>
3634

3735
/**
3836
* Allows overriding the current route returned by `useRoute` in tests. rl

packages/router/src/router.ts

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ import {
1313
RouteLocationOptions,
1414
MatcherLocationRaw,
1515
RouteParams,
16-
RouteLocationNamedRaw,
17-
RouteLocationPathRaw,
18-
RouteLocationString,
1916
} from './types'
2017
import { RouterHistory, HistoryState, NavigationType } from './history/common'
2118
import {
@@ -71,7 +68,6 @@ import {
7168
routerViewLocationKey,
7269
} from './injectionSymbols'
7370
import { addDevtools } from './devtools'
74-
import { RouteNamedMap, RouteStaticPathMap } from './types/named'
7571

7672
/**
7773
* Internal type to define an ErrorHandler
@@ -185,9 +181,9 @@ export interface RouterOptions extends PathParserOptions {
185181
}
186182

187183
/**
188-
* Router instance. **The `Options` generic is internal**.
184+
* Router instance.
189185
*/
190-
export interface Router<Options extends RouterOptions = RouterOptions> {
186+
export interface Router {
191187
/**
192188
* @internal
193189
*/
@@ -199,7 +195,7 @@ export interface Router<Options extends RouterOptions = RouterOptions> {
199195
/**
200196
* Original options object passed to create the Router
201197
*/
202-
readonly options: Options
198+
readonly options: RouterOptions
203199

204200
/**
205201
* Allows turning off the listening of history events. This is a low level api for micro-frontends.
@@ -256,25 +252,15 @@ export interface Router<Options extends RouterOptions = RouterOptions> {
256252
*
257253
* @param to - Route ___location to navigate to
258254
*/
259-
push(
260-
to:
261-
| RouteLocationNamedRaw<RouteNamedMap<Options['routes']>>
262-
| RouteLocationString<RouteStaticPathMap<Options['routes']>>
263-
| RouteLocationPathRaw<RouteStaticPathMap<Options['routes']>>
264-
): Promise<NavigationFailure | void | undefined>
255+
push(to: RouteLocationRaw): Promise<NavigationFailure | void | undefined>
265256

266257
/**
267258
* Programmatically navigate to a new URL by replacing the current entry in
268259
* the history stack.
269260
*
270261
* @param to - Route ___location to navigate to
271262
*/
272-
replace(
273-
to:
274-
| RouteLocationNamedRaw<RouteNamedMap<Options['routes']>>
275-
| RouteLocationString<RouteStaticPathMap<Options['routes']>>
276-
| RouteLocationPathRaw<RouteStaticPathMap<Options['routes']>>
277-
): Promise<NavigationFailure | void | undefined>
263+
replace(to: RouteLocationRaw): Promise<NavigationFailure | void | undefined>
278264

279265
/**
280266
* Go back in history if possible by calling `history.back()`. Equivalent to
@@ -373,9 +359,7 @@ export interface Router<Options extends RouterOptions = RouterOptions> {
373359
*
374360
* @param options - {@link RouterOptions}
375361
*/
376-
export function createRouter<Options extends RouterOptions>(
377-
options: Options
378-
): Router<Options> {
362+
export function createRouter(options: RouterOptions): Router {
379363
const matcher = createRouterMatcher(options.routes, options)
380364
const parseQuery = options.parseQuery || originalParseQuery
381365
const stringifyQuery = options.stringifyQuery || originalStringifyQuery
@@ -1173,7 +1157,7 @@ export function createRouter<Options extends RouterOptions>(
11731157
let started: boolean | undefined
11741158
const installedApps = new Set<App>()
11751159

1176-
const router: Router<Options> = {
1160+
const router: Router = {
11771161
currentRoute,
11781162
listening: true,
11791163

packages/router/src/typedRouter.ts

Lines changed: 0 additions & 28 deletions
This file was deleted.

packages/router/src/types/index.ts

Lines changed: 13 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@ import { Ref, ComponentPublicInstance, Component, DefineComponent } from 'vue'
44
import { RouteRecord, RouteRecordNormalized } from '../matcher/types'
55
import { HistoryState } from '../history/common'
66
import { NavigationFailure } from '../errors'
7-
import {
8-
RouteNamedInfo,
9-
RouteNamedMapGeneric,
10-
RouteStaticPathMapGeneric,
11-
} from './named'
12-
import { LiteralUnion } from './utils'
137

148
export type Lazy<T> = () => Promise<T>
159
export type Override<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U
@@ -73,25 +67,16 @@ export interface MatcherLocationAsName {
7367
/**
7468
* @internal
7569
*/
76-
export interface LocationAsRelativeRaw<
77-
Name extends RouteRecordName = RouteRecordName,
78-
Info extends RouteNamedInfo = RouteNamedInfo
79-
> {
80-
name?: Name
81-
params?: Info extends RouteNamedInfo<any, any, infer ParamsRaw>
82-
? ParamsRaw
83-
: RouteParamsRaw
70+
export interface LocationAsRelativeRaw {
71+
name?: string
72+
params?: RouteParamsRaw
8473
}
8574

8675
/**
8776
* @internal
8877
*/
89-
export interface MatcherLocationAsRelative<
90-
Info extends RouteNamedInfo = RouteNamedInfo
91-
> {
92-
params?: Info extends RouteNamedInfo<any, infer Params, any>
93-
? Params
94-
: RouteParams
78+
export interface MatcherLocationAsRelative {
79+
params?: RouteParams
9580
}
9681

9782
/**
@@ -124,59 +109,25 @@ export type RouteLocationRaw =
124109
| RouteLocationPathRaw
125110
| RouteLocationNamedRaw
126111

127-
/**
128-
* Route ___location that can infer full path locations
129-
*
130-
* @internal
131-
*/
132-
export type RouteLocationString<
133-
RouteMap extends RouteStaticPathMapGeneric = RouteStaticPathMapGeneric
134-
> = RouteStaticPathMapGeneric extends RouteMap
135-
? string
136-
: LiteralUnion<
137-
{
138-
[K in keyof RouteMap]: RouteMap[K]
139-
}[keyof RouteMap],
140-
string
141-
>
142-
143112
/**
144113
* Route Location that can infer the necessary params based on the name.
145114
*
146115
* @internal
147116
*/
148-
export type RouteLocationNamedRaw<
149-
RouteMap extends RouteNamedMapGeneric = RouteNamedMapGeneric
150-
> = RouteNamedMapGeneric extends RouteMap
151-
? // allows assigning a RouteLocationRaw to RouteLocationNamedRaw
152-
RouteQueryAndHash & LocationAsRelativeRaw & RouteLocationOptions
153-
: {
154-
[K in Extract<keyof RouteMap, RouteRecordName>]: LocationAsRelativeRaw<
155-
K,
156-
RouteMap[K]
157-
>
158-
}[Extract<keyof RouteMap, RouteRecordName>] &
159-
RouteQueryAndHash &
160-
RouteLocationOptions
117+
export interface RouteLocationNamedRaw
118+
extends RouteQueryAndHash,
119+
LocationAsRelativeRaw,
120+
RouteLocationOptions {}
161121

162122
/**
163123
* Route Location that can infer the possible paths.
164124
*
165125
* @internal
166126
*/
167-
export type RouteLocationPathRaw<
168-
RouteMap extends RouteStaticPathMapGeneric = RouteStaticPathMapGeneric
169-
> = RouteStaticPathMapGeneric extends RouteMap
170-
? // allows assigning a RouteLocationRaw to RouteLocationPath
171-
RouteQueryAndHash & MatcherLocationAsPath & RouteLocationOptions
172-
: RouteQueryAndHash &
173-
RouteLocationOptions &
174-
MatcherLocationAsPath<
175-
LiteralUnion<
176-
{ [K in keyof RouteMap]: RouteMap[K] }[keyof RouteMap],
177-
string
178-
>
179-
>
127+
export interface RouteLocationPathRaw
128+
extends RouteQueryAndHash,
129+
MatcherLocationAsPath,
130+
RouteLocationOptions {}
180131

181132
export interface RouteLocationMatched extends RouteRecordNormalized {
182133
// components cannot be Lazy<RouteComponent>

0 commit comments

Comments
 (0)