Type safe path utility for Next.js
yarn add next-typed-pages
# or
npm i -S next-typed-pages
- Files in
pages
directory
- pages/
- about.tsx
- users/
- index.tsx
- [userId]/
- index.tsx
- posts/
- [postId]/
- index.tsx
- settings
- index.tsx
- lang.tsx
- Execute command
next-typed-pages generate src/next-pages.ts
- Generated file content (src/next-pages.ts)
import { $route, nextPages } from 'next-typed-pages'
export const pages = nextPages({
about: $route,
users: {
index: $route,
'[userId]': {
index: $route,
posts: {
'[postId]': {
index: $route,
},
},
settings: {
index: $route,
lang: $route,
},
},
},
})
- Generate route paths type-safely
pages.index // => '/'
pages.about // => '/about'
pages.users.index // => '/users'
pages.users(null).index // => '/users/[userId]'
pages.users('123').index // => '/users/123'
pages.users('123').posts('456').index // => '/users/123/posts/456'
pages.users('123').settings.index // => '/users/123/settings'
pages.users('123').settings.lang // => '/users/123/settings/lang'
--dir
,-d
: Path to pages directory (default: src/pages)