Unverified Commit 3b4594f6 authored by Sukka's avatar Sukka Committed by GitHub

types(#42003): better typing for `next/link` (#42117)

<!--
Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change that you're making:
-->

The PR closes #42003.

Replace `(e: any) => void` with
`React.MouseEventHandler<HTMLAnchorElement>` and
`React.TouchEventHandler<HTMLAnchorElement>`. The original typing
overlap issue has also been fixed.

## Bug

- [x] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`
parent 9c2c8587
...@@ -83,21 +83,18 @@ type InternalLinkProps = { ...@@ -83,21 +83,18 @@ type InternalLinkProps = {
* @see https://github.com/vercel/next.js/commit/489e65ed98544e69b0afd7e0cfc3f9f6c2b803b7 * @see https://github.com/vercel/next.js/commit/489e65ed98544e69b0afd7e0cfc3f9f6c2b803b7
*/ */
legacyBehavior?: boolean legacyBehavior?: boolean
// e: any because as it would otherwise overlap with existing types
/** /**
* Optional event handler for when the mouse pointer is moved onto Link * Optional event handler for when the mouse pointer is moved onto Link
*/ */
onMouseEnter?: (e: any) => void onMouseEnter?: React.MouseEventHandler<HTMLAnchorElement>
// e: any because as it would otherwise overlap with existing types
/** /**
* Optional event handler for when Link is touched. * Optional event handler for when Link is touched.
*/ */
onTouchStart?: (e: any) => void onTouchStart?: React.TouchEventHandler<HTMLAnchorElement>
// e: any because as it would otherwise overlap with existing types
/** /**
* Optional event handler for when Link is clicked. * Optional event handler for when Link is clicked.
*/ */
onClick?: (e: any) => void onClick?: React.MouseEventHandler<HTMLAnchorElement>
} }
// TODO-APP: Include the full set of Anchor props // TODO-APP: Include the full set of Anchor props
...@@ -520,14 +517,14 @@ const Link = React.forwardRef<HTMLAnchorElement, LinkPropsReal>( ...@@ -520,14 +517,14 @@ const Link = React.forwardRef<HTMLAnchorElement, LinkPropsReal>(
]) ])
const childProps: { const childProps: {
onTouchStart: React.TouchEventHandler onTouchStart: React.TouchEventHandler<HTMLAnchorElement>
onMouseEnter: React.MouseEventHandler onMouseEnter: React.MouseEventHandler<HTMLAnchorElement>
onClick: React.MouseEventHandler onClick: React.MouseEventHandler<HTMLAnchorElement>
href?: string href?: string
ref?: any ref?: any
} = { } = {
ref: setRef, ref: setRef,
onClick: (e: React.MouseEvent) => { onClick(e) {
if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production') {
if (!e) { if (!e) {
throw new Error( throw new Error(
...@@ -569,7 +566,7 @@ const Link = React.forwardRef<HTMLAnchorElement, LinkPropsReal>( ...@@ -569,7 +566,7 @@ const Link = React.forwardRef<HTMLAnchorElement, LinkPropsReal>(
prefetchEnabled prefetchEnabled
) )
}, },
onMouseEnter: (e: React.MouseEvent) => { onMouseEnter(e) {
if (!legacyBehavior && typeof onMouseEnterProp === 'function') { if (!legacyBehavior && typeof onMouseEnterProp === 'function') {
onMouseEnterProp(e) onMouseEnterProp(e)
} }
...@@ -597,7 +594,7 @@ const Link = React.forwardRef<HTMLAnchorElement, LinkPropsReal>( ...@@ -597,7 +594,7 @@ const Link = React.forwardRef<HTMLAnchorElement, LinkPropsReal>(
bypassPrefetchedCheck: true, bypassPrefetchedCheck: true,
}) })
}, },
onTouchStart: (e: React.TouchEvent<HTMLAnchorElement>) => { onTouchStart(e) {
if (!legacyBehavior && typeof onTouchStartProp === 'function') { if (!legacyBehavior && typeof onTouchStartProp === 'function') {
onTouchStartProp(e) onTouchStartProp(e)
} }
......
...@@ -8,7 +8,7 @@ import { ...@@ -8,7 +8,7 @@ import {
import NextLink, { LinkProps } from 'next/link' import NextLink, { LinkProps } from 'next/link'
type NativeButtonProps = Omit< type NativeButtonProps = Omit<
HTMLProps<HTMLButtonElement>, HTMLProps<HTMLAnchorElement>,
'type' | 'prefix' | 'size' | 'width' | 'shape' | 'onClick' 'type' | 'prefix' | 'size' | 'width' | 'shape' | 'onClick'
> >
......
...@@ -12,7 +12,23 @@ export default function Page() { ...@@ -12,7 +12,23 @@ export default function Page() {
return ( return (
<> <>
<p>hello world</p> <p>hello world</p>
<Link href="/another">to /another</Link> <Link
href="/another"
onClick={(e) => {
console.log(e.currentTarget)
}}
>
to /another
</Link>
<Link
href="/another"
onClick={(e) => {
/** @ts-expect-error - foo does not exist on React.MouseEvent */
console.log(e.foo)
}}
>
to /another
</Link>
</> </>
) )
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment