You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/01-app/03-api-reference/05-config/02-typescript.mdx
+70-10Lines changed: 70 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -150,20 +150,44 @@ Next.js will generate a link definition in `.next/types` that contains informati
150
150
```
151
151
{/* prettier-ignore-end */}
152
152
153
-
Currently, experimental support includes any string literal, including dynamic segments. For non-literal strings, you currently need to manually cast the `href` with `as Route`:
153
+
Currently, support includes any string literal, including dynamic segments. For non-literal strings, you need to manually cast with `as Route`. The example below shows both `next/link` and `next/navigation` usage:
154
154
155
-
```tsx
156
-
importtype { Route } from'next';
155
+
```tsx filename="app/example-client.tsx"
156
+
'use client'
157
+
158
+
importtype { Route } from'next'
157
159
importLinkfrom'next/link'
160
+
import { useRouter } from'next/navigation'
158
161
159
-
// No TypeScript errors if href is a valid route
160
-
<Linkhref="/about" />
161
-
<Linkhref="/blog/nextjs" />
162
-
<Linkhref={`/blog/${slug}`} />
163
-
<Linkhref={('/blog'+slug) asRoute} />
162
+
exportdefaultfunction Example() {
163
+
const router =useRouter()
164
+
const slug ='nextjs'
164
165
165
-
// TypeScript errors if href is not a valid route
166
-
<Linkhref="/aboot" />
166
+
return (
167
+
<>
168
+
{/* Link: literal and dynamic */}
169
+
<Linkhref="/about" />
170
+
<Linkhref={`/blog/${slug}`} />
171
+
<Linkhref={('/blog'+slug) asRoute} />
172
+
{/* TypeScript error if href is not a valid route */}
173
+
<Linkhref="/aboot" />
174
+
175
+
{/* Router: literal and dynamic strings are validated */}
You can also type a simple data structure and iterate to render links:
231
+
232
+
```ts filename="components/nav-items.ts"
233
+
importtype { Route } from'next'
234
+
235
+
typeNavItem<Textendsstring=string> = {
236
+
href:T
237
+
label:string
238
+
}
239
+
240
+
exportconst navItems:NavItem<Route>[] = [
241
+
{ href: '/', label: 'Home' },
242
+
{ href: '/about', label: 'About' },
243
+
{ href: '/blog', label: 'Blog' },
244
+
]
245
+
```
246
+
247
+
Then, map over the items to render `Link`s:
248
+
249
+
```tsx filename="components/nav.tsx"
250
+
importLinkfrom'next/link'
251
+
import { navItems } from'./nav-items'
252
+
253
+
exportfunction Nav() {
254
+
return (
255
+
<nav>
256
+
{navItems.map((item) => (
257
+
<Linkkey={item.href}href={item.href}>
258
+
{item.label}
259
+
</Link>
260
+
))}
261
+
</nav>
262
+
)
263
+
}
264
+
```
265
+
206
266
> **How does it work?**
207
267
>
208
268
> When running `next dev` or `next build`, Next.js generates a hidden `.d.ts` file inside `.next` that contains information about all existing routes in your application (all valid routes as the `href` type of `Link`). This `.d.ts` file is included in `tsconfig.json` and the TypeScript compiler will check that `.d.ts` and provide feedback in your editor about invalid links.
0 commit comments