v2.0.0
What's Changed
- Route type generation can now also be generated outside of
next build/next devby usingnpx nextjs-routes. router.querytypes must now be narrowed usingrouter.isReady. This ensures types are correct for pages that use Automatic Static Optimization.
Next's documentation notes the following:
During prerendering, the router's query object will be empty since we do not have query information to provide during this phase. After hydration, Next.js will trigger an update to your application to provide the route parameters in the query object.
See #117 for more context and discussion.
import { useRouter } from "next/router";
const router = useRouter<"/foos/[foo]">();
// query is now typed as `{ foo?: string | undefined }`. Previously this was `{ foo: string }` which was not safe for static optimized pages
router.query;Updating useRouter call sites to check isReady will narrow the query type:
import { useRouter } from "next/router";
const router = useRouter<"/foos/[foo]">();
// query is now typed as `{ foo?: string | undefined }`. Previously this was `{ foo: string }` which was not safe for static optimized pages
router.query;
+ if (router.isReady) {
+ // query is typed as `{ foo: string }`
+ router.query;
+ }Full Changelog: v1.0.9...v2.0.0