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,29 +108,30 @@ function createRouteDefinitions() { ...@@ -108,29 +108,30 @@ function createRouteDefinitions() {
!edgeRouteTypes.length && !nodeRouteTypes.length ? 'string' : '' !edgeRouteTypes.length && !nodeRouteTypes.length ? 'string' : ''
return ` return `
type SearchOrHash = \`?\${string}\` | \`#\${string}\`
type Suffix = '' | SearchOrHash
type SafeSlug<S extends string> = type SafeSlug<S extends string> =
S extends \`\${string}/\${string}\` S extends \`\${string}/\${string}\`
? never ? never
: S extends \`\${string}?\${string}\` : S extends \`\${string}\${SearchOrHash}\`
? never ? never
: S extends '' : S extends ''
? never ? never
: S : S
type CatchAllSlug<S extends string> = type CatchAllSlug<S extends string> =
S extends \`\${string}?\${string}\` S extends \`\${string}\${SearchOrHash}\`
? never ? never
: S extends '' : S extends ''
? never ? never
: S : S
type OptionalCatchAllSlug<S extends string> = type OptionalCatchAllSlug<S extends string> =
S extends \`\${string}?\${string}\` S extends \`\${string}\${SearchOrHash}\`
? never ? never
: S : S
import { UrlObject } from 'url'
type Route<T extends string = string> = ${fallback} type Route<T extends string = string> = ${fallback}
${ ${
edgeRouteTypes.map((route) => ` | ${route}`).join('\n') + edgeRouteTypes.map((route) => ` | ${route}`).join('\n') +
...@@ -138,15 +139,33 @@ ${ ...@@ -138,15 +139,33 @@ ${
} }
declare module 'next/link' { declare module 'next/link' {
export default function Link<T extends string>(props: { import React from 'react'
[key: string]: any 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. * 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 * https://nextjs.org/docs/api-reference/next/link
*/ */
href: Route<T> | UrlObject href: T
}): JSX.Element } : {
/**
* 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: never
}
export type LinkProps<T> = LinkRestProps & HrefProp<T>
export default function Link<RouteType>(props: LinkProps<RouteType>): JSX.Element
} }
declare module 'next' { declare module 'next' {
...@@ -214,7 +233,7 @@ export class FlightTypesPlugin { ...@@ -214,7 +233,7 @@ export class FlightTypesPlugin {
} }
;(this.isEdgeServer ? edgeRouteTypes : nodeRouteTypes).push( ;(this.isEdgeServer ? edgeRouteTypes : nodeRouteTypes).push(
`\`${route}\${'' | \`?\${string}\`}\`` `\`${route}\${Suffix}\``
) )
} }
......
import type { Route } from 'next' import type { Route } from 'next'
import Link from 'next/link' import Link from 'next/link'
function Card({ href }: { href: Route | URL }) { function Card<T extends string>({ href }: { href: Route<T> | URL }) {
return ( return (
<Link href={href}> <Link href={href}>
<div>My Card</div> <div>My Card</div>
...@@ -12,25 +12,41 @@ function Card({ href }: { href: Route | URL }) { ...@@ -12,25 +12,41 @@ function Card({ href }: { href: Route | URL }) {
export default function page() { export default function page() {
const test = 'a/b' const test = 'a/b'
return ( const shouldFail = (
<> <>
<Card href="/aaa" />
<Card href="/dashboard" /> <Card href="/dashboard" />
<Link href="/aaa" /> <Card href="/blog/a/b/c/d" />
<Link href="/aaa?q=1" /> <Link href="/typing">test</Link>
<Link href="/typing" /> <Link href="/button">test</Link>
<Link href="/button" /> <Link href="/buttooon">test</Link>
<Link href="/buttooon" /> <Link href="/blog/">test</Link>
<Link href="/blog/" /> <Link href="/blog/v/w/z">test</Link>
<Link href="/blog/a/b" /> <Link href="/dashboard/">test</Link>
<Link href="/blog/v/w" /> <Link href={`/blog/a/${test}`}>test</Link>
<Link href="/dashboard/" /> </>
<Link href="/dashboard/123" /> )
<Link href="/dashboard/user" />
<Link href="/dashboard/user/" /> const shouldPass = (
<Link href="/dashboard/user/x" /> <>
<Link href={`/blog/${test}`} /> <Card href="/aaa" />
<Link href={`/blog/a/${test}`} /> <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