Skip to content

Commit 8f35c6d

Browse files
hodimbokomposva
andauthored
fix(routerlink): allow event handlers under strictTemplates (fix #2484) (#2548)
Co-authored-by: Eduardo San Martin Morote <[email protected]>
1 parent ccb5e18 commit 8f35c6d

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

packages/router/src/RouterLink.ts

Lines changed: 22 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'
@@ -359,18 +360,34 @@ export const RouterLinkImpl = /*#__PURE__*/ defineComponent({
359360
*/
360361
export const RouterLink: _RouterLinkI = RouterLinkImpl as any
361362

363+
/**
364+
* @internal
365+
*/
366+
type _RouterLinkPropsTypedBase = AllowedComponentProps &
367+
ComponentCustomProps &
368+
VNodeProps &
369+
RouterLinkProps
370+
371+
/**
372+
* @internal
373+
*/
374+
type RouterLinkPropsTyped<Custom extends boolean | undefined> =
375+
Custom extends true
376+
? _RouterLinkPropsTypedBase & { custom: true }
377+
: _RouterLinkPropsTypedBase & { custom?: false | undefined } & Omit<
378+
AnchorHTMLAttributes,
379+
'href'
380+
>
381+
362382
/**
363383
* Typed version of the `RouterLink` component. Its generic defaults to the typed router, so it can be inferred
364384
* automatically for JSX.
365385
*
366386
* @internal
367387
*/
368388
export interface _RouterLinkI {
369-
new (): {
370-
$props: AllowedComponentProps &
371-
ComponentCustomProps &
372-
VNodeProps &
373-
RouterLinkProps
389+
new <Custom extends boolean | undefined = boolean | undefined>(): {
390+
$props: RouterLinkPropsTyped<Custom>
374391

375392
$slots: {
376393
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)