Skip to content

Commit 9248c9f

Browse files
committed
fix(routerlink): allow event handlers under strictTemplates (fix #2484)
1 parent 6171856 commit 9248c9f

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

packages/router/src/RouterLink.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
// @ts-ignore
2727
ComponentOptionsMixin,
2828
MaybeRef,
29+
AnchorHTMLAttributes,
2930
} from 'vue'
3031
import { isSameRouteLocationParams, isSameRouteRecord } from './location'
3132
import { routerKey, routeLocationKey } from './injectionSymbols'
@@ -365,12 +366,20 @@ export const RouterLink: _RouterLinkI = RouterLinkImpl as any
365366
*
366367
* @internal
367368
*/
369+
type LinkAnchorAttrs = Omit<AnchorHTMLAttributes, 'href'>
370+
371+
type _BaseRouterLinkProps = AllowedComponentProps &
372+
ComponentCustomProps &
373+
VNodeProps &
374+
RouterLinkProps
375+
376+
type RouterLinkTypedProps<C extends boolean | undefined> = C extends true
377+
? _BaseRouterLinkProps & { custom: true }
378+
: _BaseRouterLinkProps & { custom?: false | undefined } & LinkAnchorAttrs
379+
368380
export interface _RouterLinkI {
369-
new (): {
370-
$props: AllowedComponentProps &
371-
ComponentCustomProps &
372-
VNodeProps &
373-
RouterLinkProps
381+
new <C extends boolean | undefined = boolean | undefined>(): {
382+
$props: RouterLinkTypedProps<C>
374383

375384
$slots: {
376385
default?: ({

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ describe('Components', () => {
2727
expectTypeOf<JSX.Element>(<RouterLink class="link" to="/foo" />)
2828
expectTypeOf<JSX.Element>(<RouterLink to={{ path: '/foo' }} />)
2929
expectTypeOf<JSX.Element>(<RouterLink to={{ path: '/foo' }} custom />)
30+
// event handlers and anchor attrs are allowed when not custom
31+
expectTypeOf<JSX.Element>(
32+
<RouterLink to="/" onFocus={() => {}} onClick={() => {}} />
33+
)
34+
expectTypeOf<JSX.Element>(
35+
<RouterLink to="/" target="_blank" rel="noopener" />
36+
)
37+
// @ts-expect-error: href is intentionally omitted
38+
expectError(<RouterLink to="/" href="/bar" />)
39+
// @ts-expect-error: onFocus should not be allowed with custom
40+
expectError(<RouterLink to="/" custom onFocus={() => {}} />)
3041

3142
// RouterView
3243
expectTypeOf<JSX.Element>(<RouterView class="view" />)

0 commit comments

Comments
 (0)