Skip to content

Commit f2b2ef6

Browse files
committed
types: typed routes for RouterLink
1 parent d02d19f commit f2b2ef6

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

packages/router/src/RouterLink.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ import { RouteRecord } from './matcher/types'
4141
import { NavigationFailure } from './errors'
4242
import { isArray, isBrowser, noop } from './utils'
4343
import type { Router } from './router'
44-
import { RouteNamedMap, RouteStaticPathMap } from './types/named'
44+
import type { RouteNamedMap, RouteStaticPathMap } from './types/named'
45+
import type { RouterTyped } from './typedRouter'
4546

4647
export interface RouterLinkOptions<
4748
Routes extends RouteLocationRaw = RouteLocationRaw
@@ -262,7 +263,11 @@ export const RouterLinkImpl = /*#__PURE__*/ defineComponent({
262263
*/
263264
export const RouterLink = RouterLinkImpl as unknown as RouterLinkTyped
264265

265-
export interface RouterLinkTyped<R extends Router = Router> {
266+
/**
267+
* Typed version of the `RouterLink` component. Its generic defaults to the typed router so it can be inferred
268+
* automatically for JSX.
269+
*/
270+
export interface RouterLinkTyped<R extends Router = RouterTyped> {
266271
new (): {
267272
$props: AllowedComponentProps &
268273
ComponentCustomProps &

packages/router/src/globalExtensions.ts

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

1010
declare module '@vue/runtime-core' {
1111
export interface ComponentCustomOptions {
@@ -60,6 +60,6 @@ declare module '@vue/runtime-core' {
6060

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

packages/router/test-dts/components.test-d.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,19 @@ let router = createRouter({
1313
})
1414

1515
// RouterLink
16-
// @ts-expect-error
16+
// @ts-expect-error missing to
1717
expectError(<RouterLink />)
18-
// @ts-expect-error
18+
// @ts-expect-error: invalid prop
1919
expectError(<RouterLink to="/" custom="text" />)
20-
// @ts-expect-error
20+
// @ts-expect-error: invalid prop
2121
expectError(<RouterLink to="/" replace="text" />)
2222
expectType<JSX.Element>(<RouterLink to="/foo" replace />)
2323
expectType<JSX.Element>(<RouterLink to="/foo" />)
2424
expectType<JSX.Element>(<RouterLink class="link" to="/foo" />)
2525
expectType<JSX.Element>(<RouterLink to={{ path: '/foo' }} />)
2626
expectType<JSX.Element>(<RouterLink to={{ path: '/foo' }} custom />)
27+
// @ts-expect-error: non existing name
28+
expectType<JSX.Element>(<RouterLink to={{ name: 'nope' }} custom />)
2729

2830
// RouterView
2931
expectType<JSX.Element>(<RouterView class="view" />)

0 commit comments

Comments
 (0)