Unverified Commit c970d951 authored by Ben Gubler's avatar Ben Gubler Committed by GitHub

feat: stabilize experimental.typedRoutes flag (#82762)

- Rename `experimental.typedRoutes` to `typedRoutes` 
- Remove old references to `typedRoutes` not working with Turbopack
- Remove unnecessary encoding of `typedRoutes` for bundlers
parent 65cf21c9
......@@ -795,10 +795,7 @@ pub struct ExperimentalConfig {
swc_trace_profiling: Option<bool>,
/// @internal Used by the Next.js internals only.
trust_host_header: Option<bool>,
/// Generate Route types and enable type checking for Link and Router.push,
/// etc. This option requires `appDir` to be enabled first.
/// @see [api reference](https://nextjs.org/docs/app/api-reference/next-config-js/typedRoutes)
typed_routes: Option<bool>,
url_imports: Option<serde_json::Value>,
view_transition: Option<bool>,
/// This option is to enable running the Webpack build in a worker thread
......
---
title: typedRoutes
description: Enable experimental support for statically typed links.
version: experimental
description: Enable support for statically typed links.
---
Experimental support for [statically typed links](/docs/app/api-reference/config/typescript#statically-typed-links). This feature requires using the App Router as well as TypeScript in your project.
> **Note**: This option has been marked as stable, so you should use `typedRoutes` instead of `experimental.typedRoutes`.
Support for [statically typed links](/docs/app/api-reference/config/typescript#statically-typed-links). This feature requires using TypeScript in your project.
```js filename="next.config.js"
/** @type {import('next').NextConfig} */
const nextConfig = {
experimental: {
typedRoutes: true,
},
typedRoutes: true,
}
module.exports = nextConfig
......
......@@ -109,15 +109,13 @@ module.exports = nextConfig
Next.js can statically type links to prevent typos and other errors when using `next/link`, improving type safety when navigating between pages.
To opt-into this feature, `experimental.typedRoutes` need to be enabled and the project needs to be using TypeScript.
To opt-into this feature, `typedRoutes` need to be enabled and the project needs to be using TypeScript.
```ts filename="next.config.ts"
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {
experimental: {
typedRoutes: true,
},
typedRoutes: true,
}
export default nextConfig
......
......@@ -2310,7 +2310,6 @@ export default async function getBaseWebpackConfig(
optimizeCss: config.experimental.optimizeCss,
nextScriptWorkers: config.experimental.nextScriptWorkers,
scrollRestoration: config.experimental.scrollRestoration,
typedRoutes: config.experimental.typedRoutes,
basePath: config.basePath,
excludeDefaultMomentLocales: config.excludeDefaultMomentLocales,
assetPrefix: config.assetPrefix,
......
......@@ -219,7 +219,7 @@ type InternalLinkProps = {
// adding this to the publicly exported type currently breaks existing apps
// `RouteInferType` is a stub here to avoid breaking `typedRoutes` when the type
// isn't generated yet. It will be replaced when the webpack plugin runs.
// isn't generated yet. It will be replaced when type generation runs.
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export type LinkProps<RouteInferType = any> = InternalLinkProps
type LinkPropsRequired = RequiredKeys<LinkProps>
......
......@@ -44,7 +44,7 @@ type InternalFormProps = {
} & Omit<HTMLFormProps, 'action' | DisallowedFormProps>
// `RouteInferType` is a stub here to avoid breaking `typedRoutes` when the type
// isn't generated yet. It will be replaced when the webpack plugin runs.
// isn't generated yet. It will be replaced when type generation runs.
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export type FormProps<RouteInferType = any> = InternalFormProps
......
......@@ -118,7 +118,7 @@ type InternalLinkProps = {
// adding this to the publicly exported type currently breaks existing apps
// `RouteInferType` is a stub here to avoid breaking `typedRoutes` when the type
// isn't generated yet. It will be replaced when the webpack plugin runs.
// isn't generated yet. It will be replaced when type generation runs.
// WARNING: This should be an interface to prevent TypeScript from inlining it
// in declarations of libraries dependending on Next.js.
// Not trivial to reproduce so only convert to an interface when needed.
......
......@@ -746,6 +746,7 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() =>
tsconfigPath: z.string().min(1).optional(),
})
.optional(),
typedRoutes: z.boolean().optional(),
useFileSystemPublicRoutes: z.boolean().optional(),
// The webpack config type is unknown, use z.any() here
webpack: z.any().nullable().optional(),
......
......@@ -608,8 +608,9 @@ export interface ExperimentalConfig {
}
/**
* Generate Route types and enable type checking for Link and Router.push, etc.
* @see https://nextjs.org/docs/app/api-reference/next-config-js/typedRoutes
* Enable type checking for Link and Router.push, etc.
* @deprecated Use `typedRoutes` instead — this feature is now stable.
* @see https://nextjs.org/docs/app/api-reference/config/typescript#statically-typed-links
*/
typedRoutes?: boolean
......@@ -999,6 +1000,14 @@ export interface NextConfig extends Record<string, any> {
*/
typescript?: TypeScriptConfig
/**
* Enable type checking for Link and Router.push, etc.
* This feature requires TypeScript in your project.
*
* @see [Typed Links documentation](https://nextjs.org/docs/app/api-reference/config/typescript#statically-typed-links)
*/
typedRoutes?: boolean
/**
* Headers allow you to set custom HTTP headers for an incoming request path.
*
......@@ -1415,6 +1424,7 @@ export const defaultConfig = Object.freeze({
ignoreBuildErrors: false,
tsconfigPath: 'tsconfig.json',
},
typedRoutes: false,
distDir: '.next',
cleanDistDir: true,
assetPrefix: '',
......@@ -1560,7 +1570,6 @@ export const defaultConfig = Object.freeze({
amp: undefined,
urlImports: undefined,
turbo: undefined,
typedRoutes: false,
typedEnv: false,
clientTraceMetadata: undefined,
parallelServerCompiles: false,
......
......@@ -654,6 +654,13 @@ function assignDefaults(
configFileName,
silent
)
warnOptionHasBeenMovedOutOfExperimental(
result,
'typedRoutes',
'typedRoutes',
configFileName,
silent
)
warnOptionHasBeenMovedOutOfExperimental(
result,
'outputFileTracingRoot',
......
......@@ -348,7 +348,7 @@ export async function writeRouteTypesManifest(
await fs.promises.writeFile(filePath, generateRouteTypesFile(manifest))
// Write the link.d.ts file if typedRoutes is enabled
if (config.experimental?.typedRoutes === true) {
if (config.typedRoutes === true) {
const linkTypesPath = path.join(dirname, 'link.d.ts')
await fs.promises.writeFile(linkTypesPath, generateLinkTypesFile(manifest))
}
......
......@@ -55,7 +55,7 @@ export type { Instrumentation } from './server/instrumentation/types'
*/
// `RouteInferType` is a stub here to avoid breaking `typedRoutes` when the type
// isn't generated yet. It will be replaced when the webpack plugin runs.
// isn't generated yet. It will be replaced when type generation runs.
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export type Route<RouteInferType = any> = string & {}
......
......@@ -2,9 +2,7 @@
* @type {import('next').NextConfig}
*/
const nextConfig = {
experimental: {
typedRoutes: true,
},
typedRoutes: true,
async redirects() {
return [
{
......
......@@ -3,9 +3,7 @@ import createMdx from '@next/mdx'
const withMdx = createMdx()
export default withMdx({
experimental: {
typedRoutes: true,
},
typedRoutes: true,
pageExtensions: ['js', 'jsx', 'ts', 'tsx', 'mdx'],
......
/** @type {import('next').NextConfig} */
module.exports = {
typedRoutes: true,
experimental: {
typedRoutes: true,
webpackBuildWorker: true,
},
}
/** @type {import('next').NextConfig} */
module.exports = {
typedRoutes: true,
experimental: {
typedRoutes: true,
parallelServerBuildTraces: true,
webpackBuildWorker: true,
},
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment