Skip to content

Commit

Permalink
Add special route types to the link type declaration (vercel#50066)
Browse files Browse the repository at this point in the history
This PR adds generated type definitions for `Link` hrefs with only search, hash or with a protocol.

Closes vercel#49825.
fix NEXT-1203
  • Loading branch information
shuding authored and hydRAnger committed Jun 12, 2023
1 parent 7286132 commit f5092a9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/next/src/build/webpack/plugins/next-types-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ function createRouteDefinitions() {
*/
declare namespace __next_route_internal_types__ {
type SearchOrHash = \`?\${string}\` | \`#\${string}\`
type WithProtocol = \`\${string}:\${string}\`
type Suffix = '' | SearchOrHash
Expand Down Expand Up @@ -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)
`
Expand Down
5 changes: 5 additions & 0 deletions test/integration/app-types/src/app/type-checks/link/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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>
</>
)

Expand Down

0 comments on commit f5092a9

Please sign in to comment.