Unverified Commit 5727d48d authored by Shu Ding's avatar Shu Ding Committed by GitHub

Fix hash in route types (#45820)

This PR fixes the issue that link with hash suffix can't be type checked. It also improves the experience of implementing a custom component that uses `Route` (you'll have to add a generic type that extends `string`, of course):

<img width="552" alt="CleanShot-2023-02-13-oyakgbw9@2x" src="https://user-images.githubusercontent.com/3676859/218458034-126337c7-14c3-4dc4-9a5d-27269d9c99b5.png">

Also, we need to improve out tests to expose all type diagnostics instead of only the first one.

## Bug

- [ ] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] [e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent 31cd027d
......@@ -108,28 +108,29 @@ function createRouteDefinitions() {
!edgeRouteTypes.length && !nodeRouteTypes.length ? 'string' : ''
return `
type SearchOrHash = \`?\${string}\` | \`#\${string}\`
type Suffix = '' | SearchOrHash
type SafeSlug<S extends string> =
S extends \`\${string}/\${string}\`
? never
: S extends \`\${string}?\${string}\`
: S extends \`\${string}\${SearchOrHash}\`
? never
: S extends ''
? never
: S
type CatchAllSlug<S extends string> =
S extends \`\${string}?\${string}\`
S extends \`\${string}\${SearchOrHash}\`
? never
: S extends ''
? never
: S
type OptionalCatchAllSlug<S extends string> =
S extends \`\${string}?\${string}\`
S extends \`\${string}\${SearchOrHash}\`
? never
: S
import { UrlObject } from 'url'
type Route<T extends string = string> = ${fallback}
${
......@@ -138,15 +139,33 @@ ${
}
declare module 'next/link' {
export default function Link<T extends string>(props: {
[key: string]: any
import React from 'react'
import { UrlObject } from 'url'
import { LinkProps as OriginalLinkProps } from 'next/dist/client/link'
type LinkRestProps = Omit<OriginalLinkProps, 'href'> & {
children: React.ReactNode
}
// If the href prop can be a Route type with an infer-able S, it's valid.
type HrefProp<T> = T extends (Route<infer S> | UrlObject) ? {
/**
* The path or URL to navigate to. This is the only required prop. It can also be an object.
*
* https://nextjs.org/docs/api-reference/next/link
*/
href: T
} : {
/**
* The path or URL to navigate to. This is the only required prop. It can also be an object.
*
*
* https://nextjs.org/docs/api-reference/next/link
*/
href: Route<T> | UrlObject
}): JSX.Element
href: never
}
export type LinkProps<T> = LinkRestProps & HrefProp<T>
export default function Link<RouteType>(props: LinkProps<RouteType>): JSX.Element
}
declare module 'next' {
......@@ -214,7 +233,7 @@ export class FlightTypesPlugin {
}
;(this.isEdgeServer ? edgeRouteTypes : nodeRouteTypes).push(
`\`${route}\${'' | \`?\${string}\`}\``
`\`${route}\${Suffix}\``
)
}
......
import type { Route } from 'next'
import Link from 'next/link'
function Card({ href }: { href: Route | URL }) {
function Card<T extends string>({ href }: { href: Route<T> | URL }) {
return (
<Link href={href}>
<div>My Card</div>
......@@ -12,25 +12,41 @@ function Card({ href }: { href: Route | URL }) {
export default function page() {
const test = 'a/b'
return (
const shouldFail = (
<>
<Card href="/aaa" />
<Card href="/dashboard" />
<Link href="/aaa" />
<Link href="/aaa?q=1" />
<Link href="/typing" />
<Link href="/button" />
<Link href="/buttooon" />
<Link href="/blog/" />
<Link href="/blog/a/b" />
<Link href="/blog/v/w" />
<Link href="/dashboard/" />
<Link href="/dashboard/123" />
<Link href="/dashboard/user" />
<Link href="/dashboard/user/" />
<Link href="/dashboard/user/x" />
<Link href={`/blog/${test}`} />
<Link href={`/blog/a/${test}`} />
<Card href="/blog/a/b/c/d" />
<Link href="/typing">test</Link>
<Link href="/button">test</Link>
<Link href="/buttooon">test</Link>
<Link href="/blog/">test</Link>
<Link href="/blog/v/w/z">test</Link>
<Link href="/dashboard/">test</Link>
<Link href={`/blog/a/${test}`}>test</Link>
</>
)
const shouldPass = (
<>
<Card href="/aaa" />
<Link href="/about">test</Link>
<Link href="/aaa#aaa">test</Link>
<Link href="/aaa?q=1">test</Link>
<Link href="/blog/a/b">test</Link>
<Link href="/blog/v/w">test</Link>
<Link href="/dashboard/123">test</Link>
<Link href="/dashboard/user">test</Link>
<Link href="/dashboard/user/">test</Link>
<Link href="/dashboard/user/x">test</Link>
<Link href={`/blog/${test}`}>test</Link>
<Link href={('/blog/' + test) as Route}>test</Link>
</>
)
return (
<>
{shouldFail}
{shouldPass}
</>
)
}
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