Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ test/e2e/app-dir/server-source-maps/fixtures/default/internal-pkg/ignored.js
test/e2e/app-dir/server-source-maps/fixtures/default/internal-pkg/sourcemapped.js
test/e2e/app-dir/server-source-maps/fixtures/default/external-pkg/sourcemapped.js
test/development/mcp-server/fixtures/default-template/app/build-error/page.tsx
# Tested against auto-generated content that isn't formatted
test/integration/typescript-app-type-declarations/next-env.strictRouteTypes.d.ts

# turbopack crates, disable for some tests and precompiled dependencies.
/turbopack/crates/*/js/src/compiled
Expand Down
3 changes: 3 additions & 0 deletions packages/next/src/build/type-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function verifyTypeScriptSetup(
strictRouteTypes: boolean,
typeCheckPreflight: boolean,
tsconfigPath: string | undefined,
typedRoutes: boolean,
disableStaticImages: boolean,
cacheDir: string | undefined,
enableWorkerThreads: boolean | undefined,
Expand Down Expand Up @@ -54,6 +55,7 @@ function verifyTypeScriptSetup(
strictRouteTypes,
typeCheckPreflight,
tsconfigPath,
typedRoutes,
disableStaticImages,
cacheDir,
hasAppDir,
Expand Down Expand Up @@ -122,6 +124,7 @@ export async function startTypeChecking({
Boolean(config.experimental.strictRouteTypes),
!ignoreTypeScriptErrors,
config.typescript.tsconfigPath,
Boolean(config.typedRoutes),
config.images.disableStaticImages,
cacheDir,
config.experimental.workerThreads,
Expand Down
1 change: 1 addition & 0 deletions packages/next/src/cli/next-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ async function runPlaywright(
strictRouteTypes: Boolean(nextConfig.experimental.strictRouteTypes),
typeCheckPreflight: false,
tsconfigPath: nextConfig.typescript.tsconfigPath,
typedRoutes: Boolean(nextConfig.typedRoutes),
disableStaticImages: nextConfig.images.disableStaticImages,
hasAppDir: !!appDir,
hasPagesDir: !!pagesDir,
Expand Down
1 change: 1 addition & 0 deletions packages/next/src/cli/next-typegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ const nextTypegen = async (
strictRouteTypes,
typeCheckPreflight: false,
tsconfigPath: nextConfig.typescript.tsconfigPath,
typedRoutes: Boolean(nextConfig.typedRoutes),
disableStaticImages: nextConfig.images.disableStaticImages,
hasAppDir: !!appDir,
hasPagesDir: !!pagesDir,
Expand Down
38 changes: 32 additions & 6 deletions packages/next/src/lib/typescript/writeAppTypeDeclarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ export async function writeAppTypeDeclarations({
imageImportsEnabled,
hasPagesDir,
hasAppDir,
strictRouteTypes,
typedRoutes,
}: {
baseDir: string
distDir: string
imageImportsEnabled: boolean
hasPagesDir: boolean
hasAppDir: boolean
strictRouteTypes: boolean
typedRoutes: boolean
}): Promise<void> {
// Reference `next` types
const appTypeDeclarations = path.join(baseDir, 'next-env.d.ts')
Expand Down Expand Up @@ -42,17 +46,17 @@ export async function writeAppTypeDeclarations({
*
* @see https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html
*/
const directives: string[] = [
const lines: string[] = [
// Include the core Next.js typings.
'/// <reference types="next" />',
]

if (imageImportsEnabled) {
directives.push('/// <reference types="next/image-types/global" />')
lines.push('/// <reference types="next/image-types/global" />')
}

if (hasAppDir && hasPagesDir) {
directives.push(
lines.push(
'/// <reference types="next/navigation-types/compat/navigation" />'
)
}
Expand All @@ -63,16 +67,38 @@ export async function writeAppTypeDeclarations({
)

// Use ESM import instead of triple-slash reference for better ESLint compatibility
directives.push(`import "./${routeTypesPath}";`)
lines.push(`import "./${routeTypesPath}";`)

if (strictRouteTypes) {
const cacheLifePath = path.posix.join(
distDir.replaceAll(path.win32.sep, path.posix.sep),
'types/cache-life.d.ts'
)
lines.push(`import "./${cacheLifePath}";`)

const routeValidatorPath = path.posix.join(
distDir.replaceAll(path.win32.sep, path.posix.sep),
'types/validator.ts'
)
lines.push(`import "./${routeValidatorPath}";`)

if (typedRoutes === true) {
const linkTypesPath = path.posix.join(
distDir.replaceAll(path.win32.sep, path.posix.sep),
'types/link.d.ts'
)
lines.push(`import "./${linkTypesPath}";`)
}
}

// Push the notice in.
directives.push(
lines.push(
'',
'// NOTE: This file should not be edited',
`// see https://nextjs.org/docs/${hasAppDir ? 'app' : 'pages'}/api-reference/config/typescript for more information.`
)

const content = directives.join(eol) + eol
const content = lines.join(eol) + eol

// Avoids an un-necessary write on read-only fs
if (currentContent === content) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@ describe('writeConfigurationDefaults()', () => {
],
"include": [
"next-env.d.ts",
".next/types/**/*.ts",
".next/dev/types/**/*.ts",
"**/*.mts",
"**/*.ts",
"**/*.tsx",
Expand All @@ -109,7 +107,7 @@ describe('writeConfigurationDefaults()', () => {
- strict was set to false
- noEmit was set to true
- incremental was set to true
- include was set to ['next-env.d.ts', '.next/types/**/*.ts', '.next/dev/types/**/*.ts', '**/*.mts', '**/*.ts', '**/*.tsx']
- include was set to ['next-env.d.ts', '**/*.mts', '**/*.ts', '**/*.tsx']
- plugins was updated to add { name: 'next' }
- exclude was set to ['node_modules']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ export async function writeConfigurationDefaults(

if (!('include' in userTsConfig)) {
const defaultInclude =
hasAppDir || strictRouteTypes
hasAppDir && !strictRouteTypes
? ['next-env.d.ts', ...nextTypes, '**/*.mts', '**/*.ts', '**/*.tsx']
: ['next-env.d.ts', '**/*.mts', '**/*.ts', '**/*.tsx']

Expand All @@ -295,7 +295,7 @@ export async function writeConfigurationDefaults(
bold(defaultInclude.map((type) => `'${type}'`).join(', ')) +
']'
)
} else if (hasAppDir || strictRouteTypes) {
} else if (hasAppDir && !strictRouteTypes) {
const missingFromResolved = []
for (const type of nextTypes) {
if (!userTsConfig.include.includes(type)) {
Expand Down
4 changes: 4 additions & 0 deletions packages/next/src/lib/verify-typescript-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export async function verifyTypeScriptSetup({
strictRouteTypes,
tsconfigPath,
typeCheckPreflight,
typedRoutes,
disableStaticImages,
hasAppDir,
hasPagesDir,
Expand All @@ -54,6 +55,7 @@ export async function verifyTypeScriptSetup({
strictRouteTypes: boolean
tsconfigPath: string | undefined
typeCheckPreflight: boolean
typedRoutes: boolean
disableStaticImages: boolean
hasAppDir: boolean
hasPagesDir: boolean
Expand Down Expand Up @@ -149,6 +151,8 @@ export async function verifyTypeScriptSetup({
imageImportsEnabled: !disableStaticImages,
hasPagesDir,
hasAppDir,
strictRouteTypes,
typedRoutes,
})

let result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ async function verifyTypeScript(opts: SetupOpts) {
strictRouteTypes: Boolean(opts.nextConfig.experimental.strictRouteTypes),
typeCheckPreflight: false,
tsconfigPath: opts.nextConfig.typescript.tsconfigPath,
typedRoutes: Boolean(opts.nextConfig.typedRoutes),
disableStaticImages: opts.nextConfig.images.disableStaticImages,
hasAppDir: !!opts.appDir,
hasPagesDir: !!opts.pagesDir,
Expand Down
2 changes: 0 additions & 2 deletions test/e2e/tsconfig-module-preserve/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ describe('tsconfig module: preserve', () => {
},
"include": [
"next-env.d.ts",
".next/types/**/*.ts",
".next/dev/types/**/*.ts",
"**/*.mts",
"**/*.ts",
"**/*.tsx"
Expand Down
Loading
Loading