TypeScript in Next.js
The Next.js-specific TypeScript features: generated route types, PageProps and LayoutProps, typed links with typedRoutes, and typed config.
TypeScript in Next.js
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- `next build` runs the type check, so a type error fails the build; Next.js 16 requires TypeScript 5.1 or newer.
- `PageProps<"/blog/[slug]">`, `LayoutProps` and `RouteContext` are generated global helpers that tie prop types to the folder structure.
- `typedRoutes: true` makes `<Link href>` reject routes that do not exist; non-literal hrefs need an `as Route` cast.
- `params` and `searchParams` are promises since Next.js 15, so you `await` them, and the generated types make forgetting that a compile error.
What Next.js adds on top of TypeScript
You already know what TypeScript does; the TypeScript roadmap covers the language, including tsconfig and strict mode. This lesson is about what is specific to Next.js, and the answer is more than you might expect, because a Next.js app has a hidden type problem: the URLs.
Routes are defined by folders. app/blog/[slug]/page.tsx creates /blog/:slug, and its props include params. Nothing in plain TypeScript connects the folder name to the prop type or to the href in a <Link>. Next.js bridges that by generating types from your file system.
Zero setup
Rename a file to .ts or .tsx and run next dev or next build. Next.js installs the TypeScript dependencies and creates tsconfig.json for you. The generated config includes a next plugin for your editor, and include entries for next-env.d.ts and the generated .next/types folders. Next.js 16 requires TypeScript 5.1 or newer and runs the type check as part of next build. A type error fails the build, so you cannot ship one by accident. next-env.d.ts is regenerated, so never edit it; put your own declarations in a separate .d.ts file.
Route-aware type helpers
Next.js generates three global helpers, available without imports, once next dev, next build or next typegen has run:
// app/blog/[slug]/page.tsx
export default async function Page(props: PageProps<"/blog/[slug]">) {
const { slug } = await props.params;
return <h1>{slug}</h1>;
}
PageProps<"/blog/[slug]"> knows that params is a promise of { slug: string }, and also types searchParams. LayoutProps<"/"> does the same for layouts, including a typed children and any parallel route slots, and RouteContext types the second argument of a Route Handler. If you rename the folder from [slug] to [id], the type updates, and every file that used the old name stops compiling. That is exactly the kind of error you want caught at build time rather than as a blank page. The create-next-app layout uses LayoutProps<"/"> this way.
Notice the await. Since Next.js 15 and fully in 16, params and searchParams are asynchronous, so you cannot destructure them directly in the function signature. The types make forgetting that a compile error rather than a runtime surprise.
Typed links
Add one line to enable checking of href values:
// next.config.ts
const nextConfig = { typedRoutes: true };
export default nextConfig;
Now <Link href="/blog/hello"> compiles because that route exists, and <Link href="/nope"> fails. I tested this in a Next.js 16.4.6 project: the build stopped with Type '"/nope"' is not assignable to type 'UrlObject | RouteImpl<"/nope">'. For hrefs built at runtime you cast with the Route type, which is an explicit “I vouch for this string” instead of a silent hole. It works from the route manifest, so it knows static and dynamic routes alike.
Environment variables and API shapes
The docs also describe typed environment variable IntelliSense, which uses your .env files to suggest variable names, and a pattern for sharing types end to end: define the shape of your data once, use it in the Server Component that fetches it and the Client Component that renders it. For data coming from outside your program, such as a form or a request body, a type is only a promise. Validate with a runtime schema, as in validating untrusted data at runtime, then use the validated type.
Config and build speed
next.config.ts itself is typed with NextConfig, so a misspelled option is an error. For large projects, incremental type checking is supported through "incremental": true, which the generated tsconfig.json already sets. And there is an escape hatch, typescript.ignoreBuildErrors, which exists but turns off the safety net; treat it as a smell.
What the video covers
Tobi Mey’s “5 Next.js Typescript Tricks You Didn’t Know” is a 6-minute chaptered video from November 2025: the Next.js TypeScript checker, typed routes, route-aware type helpers, and typed environment variables, with a link to the official TypeScript docs. Its description links the official TypeScript docs page and opens with a sponsor line for Arcade.dev, which the lesson does not rely on. It is short and assumes you already know TypeScript; this lesson fills in the reasoning and the await rules. A longer video on TypeScript basics through a Next.js project is the runner-up.
Where this fits
With testing and types in place, the app is ready to leave your laptop. The last group covers shipping it. Next: build output and next build.
Check yourself
What does `typedRoutes: true` change for `<Link>`?
Literal `href` values are validated against your real routes, so a link to a route that does not exist fails type checking.
Why must you `await` `props.params` in a page?
Since Next.js 15 and fully in 16, `params` and `searchParams` are asynchronous, and `PageProps` types them as promises.
Why is a TypeScript type not enough for data from a form or request body?
A type is only a compile-time promise, so external data must be validated with a runtime schema before you trust the type.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.