Add special route types to the link type declaration (#50066)

This PR adds generated type definitions for `Link` hrefs with only search, hash or with a protocol.

Closes #49825.
fix NEXT-1203
This commit is contained in:
Shu Ding 2023-05-23 19:36:09 +02:00 committed by GitHub
parent 33b8e6157f
commit 1a40651267
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 0 deletions

View file

@ -362,6 +362,7 @@ function createRouteDefinitions() {
*/
declare namespace __next_route_internal_types__ {
type SearchOrHash = \`?\${string}\` | \`#\${string}\`
type WithProtocol = \`\${string}:\${string}\`
type Suffix = '' | SearchOrHash
@ -394,6 +395,8 @@ declare namespace __next_route_internal_types__ {
// This keeps autocompletion working for static routes.
'| StaticRoutes'
}
| SearchOrHash
| WithProtocol
| \`\${StaticRoutes}\${SearchOrHash}\`
| (T extends \`\${DynamicRoutes<infer _>}\${Suffix}\` ? T : never)
`

View file

@ -62,6 +62,11 @@ export default function page() {
<Link href="/redirect/v1/guides/x/page">test</Link>
<Link href="/redirect/guides/x/page">test</Link>
<Link href={new URL('/about')}>test</Link>
<Link href="https://nextjs.org">test</Link>
<Link href="http://nextjs.org">test</Link>
<Link href="#id">test</Link>
<Link href="?page=1">test</Link>
<Link href="mailto:foo@example.com">test</Link>
</>
)