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
docs: Type narrowing using LayoutProps, PageProps (#83692)
Make a couple of clarifications:
- type resolved by the helpers
- ways to narrow down to a narrower union
---------
Co-authored-by: vercel[bot] <35613825+vercel[bot]@users.noreply.github.com>
Co-authored-by: Rich Haines <[email protected]>
Copy file name to clipboardExpand all lines: docs/01-app/03-api-reference/03-file-conventions/dynamic-routes.mdx
+20Lines changed: 20 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -113,13 +113,33 @@ The difference between **catch-all** and **optional catch-all** segments is that
113
113
114
114
When using TypeScript, you can add types for `params` depending on your configured route segment — use [`PageProps<'/route'>`](/docs/app/api-reference/file-conventions/page#page-props-helper), [`LayoutProps<'/route'>`](/docs/app/api-reference/file-conventions/layout#layout-props-helper), or [`RouteContext<'/route'>`](/docs/app/api-reference/file-conventions/route#route-context-helper) to type `params` in `page`, `layout`, and `route` respectively.
115
115
116
+
Route `params` values are typed as `string`, `string[]`, or `undefined` (for optional catch-all segments), because their values aren't known until runtime. Users can enter any URL into the address bar, and these broad types help ensure that your application code handles all these possible cases.
If you're working on a route where `params` can only have a fixed number of valid values, such as a `[locale]` param with a known set of language codes, you can use runtime validation to handle any invalid params a user may enter, and let the rest of your application work with the narrower type from your known set.
126
+
127
+
```tsx filename="/app/[locale]/page.tsx"
128
+
import { notFound } from'next/navigation'
129
+
importtype { Locale } from'@i18n/types'
130
+
import { isValidLocale } from'@i18n/utils'
131
+
132
+
function assertValidLocale(value:string):assertsvalueisLocale {
0 commit comments