Capítulo 428 de 456
Full guide to Next.js's built-in TypeScript-first experience: auto-setup, next-env.d.ts, statically typed links, typed env vars, and how to configure/disable type checking.
.ts/.tsx and running next dev/next build auto-installs deps and creates tsconfig.json.tsc CLI by default (experimental.useTypeScriptCli) so no extra config is needed once TS7 is installed.next-env.d.ts: auto-generated, references Next.js types; add to .gitignore; never edit manually; regenerated by next dev, next build, or next typegen.typedRoutes: opt-in static typing for href in next/link (and next/navigation methods push/replace/prefetch in App Router). Literal strings validated automatically; non-literal strings need as Route cast.experimental.typedEnv: generates IntelliSense types for loaded env vars in .next/types during dev.typescript.tsconfigPath: use a different tsconfig for builds vs. editor (e.g. relaxed checks in CI/monorepo).typescript.ignoreBuildErrors: dangerously skip type checking entirely in production builds.next.config.ts: on Node v22.10.0+ with process.features.typescript, next.config.ts/.mts can use native ESM (top-level await, dynamic import()).const nextConfig: NextConfig = {
typedRoutes: true,
}
'use client'
import type { Route } from 'next'
import Link from 'next/link'
import { useRouter } from 'next/navigation'
export default function Example() {
const router = useRouter()
const slug = 'nextjs'
return (
<>
<Link href="/about" />
<Link href={`/blog/${slug}`} />
<Link href={('/blog/' + slug) as Route} />
<button onClick={() => router.push('/about')}>Push About</button>
<button onClick={() => router.push(('/blog/' + slug) as Route)}>Push Non-literal</button>
</>
)
}
as Route.function Card<T extends string>({ href }: { href: Route<T> | URL }) {
return <Link href={href}><div>My Card</div></Link>
}
href tipado em componente wrapper genérico de Link.| Version | Changes |
|---|---|
v15.0.0 | next.config.ts support added for TypeScript projects. |
v13.2.0 | Statically typed links available in beta. |
v12.0.0 | SWC used by default to compile TypeScript/TSX. |
v10.2.1 | Incremental type checking support added. |
next-env.d.ts manualmente: é sobrescrito automaticamente; crie um arquivo separado (ex. new-types.d.ts) e referencie no tsconfig.json.ignoreBuildErrors: true sem type-check em outro passo do pipeline: build "perigoso" que ignora erros reais de tipo..next/types/**/*.ts ao include do tsconfig quando o projeto não foi criado via create-next-app: quebra os tipos de rotas gerados.typedRoutes funciona em App e Pages Router para next/link, mas só tipa next/navigation (não next/router) no App Router.next typegen gera tipos de rota sem build completo, útil pra CI (next typegen && tsc --noEmit).tsconfig.json é observado; ao usar typescript.tsconfigPath com outro arquivo, reinicie o dev server pra aplicar mudanças.experimental.typedEnv exclui .env.production* por padrão; rode next dev com NODE_ENV=production pra incluir essas vars.ignoreBuildErrors/tsconfigPath referenciada aqui em detalhe.typedRoutes.