Commit fdf1762c authored by 速冻饺子's avatar 速冻饺子 Committed by Ben Gubler

fix: use ESM import instead of triple-slash reference for routes.d.ts (#82867)

## What?

Replace `/// <reference path="./.next/types/routes.d.ts" />` with
`import type {} from './.next/types/routes.d.ts'` in generated
`next-env.d.ts` files.

## Why?

The current implementation uses triple-slash reference directives which
trigger ESLint errors when using the
`@typescript-eslint/triple-slash-reference` rule. This rule is commonly
enabled in TypeScript projects to encourage modern ES module imports
over legacy triple-slash references.

## How?

- Modified `writeAppTypeDeclarations.ts` to generate ESM import
statements instead of triple-slash references
- Updated all create-next-app templates to use the new format
- Updated corresponding unit tests to reflect the new expected output

The change maintains identical TypeScript functionality while improving
ESLint compatibility. The `import type {}` syntax ensures type-only
imports that are properly elided during compilation.

**Testing:**
- Existing unit tests pass with updated expectations
- Verified ESLint `@typescript-eslint/triple-slash-reference` rule no
longer triggers errors
- TypeScript functionality remains unchanged

Fixes [#82828](https://github.com/vercel/next.js/issues/82828)

---------
Co-authored-by: 's avatarvercel[bot] <35613825+vercel[bot]@users.noreply.github.com>
Co-authored-by: 's avatarBen Gubler <nebrelbug@gmail.com>
parent 497ec6aa
/// <reference types="next" /> /// <reference types="next" />
/// <reference types="next/image-types/global" /> /// <reference types="next/image-types/global" />
/// <reference path="./.next/types/routes.d.ts" /> import "./.next/types/routes.d.ts";
// NOTE: This file should not be edited // NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information. // see https://nextjs.org/docs/basic-features/typescript for more information.
/// <reference types="next" /> /// <reference types="next" />
/// <reference types="next/image-types/global" /> /// <reference types="next/image-types/global" />
/// <reference path="./.next/types/routes.d.ts" /> import "./.next/types/routes.d.ts";
// NOTE: This file should not be edited // NOTE: This file should not be edited
// see https://nextjs.org/docs/app/api-reference/config/typescript for more information. // see https://nextjs.org/docs/app/api-reference/config/typescript for more information.
/// <reference types="next" /> /// <reference types="next" />
/// <reference types="next/image-types/global" /> /// <reference types="next/image-types/global" />
/// <reference path="./.next/types/routes.d.ts" /> import "./.next/types/routes.d.ts";
// NOTE: This file should not be edited // NOTE: This file should not be edited
// see https://nextjs.org/docs/app/api-reference/config/typescript for more information. // see https://nextjs.org/docs/app/api-reference/config/typescript for more information.
/// <reference types="next" /> /// <reference types="next" />
/// <reference types="next/image-types/global" /> /// <reference types="next/image-types/global" />
/// <reference path="./.next/types/routes.d.ts" /> import "./.next/types/routes.d.ts";
// NOTE: This file should not be edited // NOTE: This file should not be edited
// see https://nextjs.org/docs/app/api-reference/config/typescript for more information. // see https://nextjs.org/docs/app/api-reference/config/typescript for more information.
/// <reference types="next" /> /// <reference types="next" />
/// <reference types="next/image-types/global" /> /// <reference types="next/image-types/global" />
/// <reference path="./.next/types/routes.d.ts" /> import "./.next/types/routes.d.ts";
// NOTE: This file should not be edited // NOTE: This file should not be edited
// see https://nextjs.org/docs/app/api-reference/config/typescript for more information. // see https://nextjs.org/docs/app/api-reference/config/typescript for more information.
/// <reference types="next" /> /// <reference types="next" />
/// <reference types="next/image-types/global" /> /// <reference types="next/image-types/global" />
/// <reference path="./.next/types/routes.d.ts" /> import "./.next/types/routes.d.ts";
// NOTE: This file should not be edited // NOTE: This file should not be edited
// see https://nextjs.org/docs/pages/api-reference/config/typescript for more information. // see https://nextjs.org/docs/pages/api-reference/config/typescript for more information.
/// <reference types="next" /> /// <reference types="next" />
/// <reference types="next/image-types/global" /> /// <reference types="next/image-types/global" />
/// <reference path="./.next/types/routes.d.ts" /> import "./.next/types/routes.d.ts";
// NOTE: This file should not be edited // NOTE: This file should not be edited
// see https://nextjs.org/docs/pages/api-reference/config/typescript for more information. // see https://nextjs.org/docs/pages/api-reference/config/typescript for more information.
/// <reference types="next" /> /// <reference types="next" />
/// <reference types="next/image-types/global" /> /// <reference types="next/image-types/global" />
/// <reference path="./.next/types/routes.d.ts" /> import "./.next/types/routes.d.ts";
// NOTE: This file should not be edited // NOTE: This file should not be edited
// see https://nextjs.org/docs/pages/api-reference/config/typescript for more information. // see https://nextjs.org/docs/pages/api-reference/config/typescript for more information.
/// <reference types="next" /> /// <reference types="next" />
/// <reference types="next/image-types/global" /> /// <reference types="next/image-types/global" />
/// <reference path="./.next/types/routes.d.ts" /> import "./.next/types/routes.d.ts";
// NOTE: This file should not be edited // NOTE: This file should not be edited
// see https://nextjs.org/docs/pages/api-reference/config/typescript for more information. // see https://nextjs.org/docs/pages/api-reference/config/typescript for more information.
...@@ -62,7 +62,13 @@ export async function writeAppTypeDeclarations({ ...@@ -62,7 +62,13 @@ export async function writeAppTypeDeclarations({
'types/routes.d.ts' 'types/routes.d.ts'
) )
directives.push(`/// <reference path="./${routeTypesPath}" />`) // Ensure the path is POSIX-compliant for imports.
const routeTypesPathPosix = routeTypesPath
.split(path.sep)
.join(path.posix.sep)
// Use ESM import instead of triple-slash reference for better ESLint compatibility
directives.push(`import "./${routeTypesPathPosix}";`)
// Push the notice in. // Push the notice in.
directives.push( directives.push(
......
/// <reference types="next" /> /// <reference types="next" />
/// <reference types="next/image-types/global" /> /// <reference types="next/image-types/global" />
/// <reference path="./.next/types/routes.d.ts" /> import "./.next/types/routes.d.ts";
// NOTE: This file should not be edited // NOTE: This file should not be edited
// see https://nextjs.org/docs/pages/api-reference/config/typescript for more information. // see https://nextjs.org/docs/pages/api-reference/config/typescript for more information.
...@@ -22,7 +22,7 @@ describe('find config', () => { ...@@ -22,7 +22,7 @@ describe('find config', () => {
(imageImportsEnabled (imageImportsEnabled
? '/// <reference types="next/image-types/global" />' + eol ? '/// <reference types="next/image-types/global" />' + eol
: '') + : '') +
'/// <reference path="./.next/types/routes.d.ts" />' + `import "./.next/types/routes.d.ts";` +
eol + eol +
eol + eol +
'// NOTE: This file should not be edited' + '// NOTE: This file should not be edited' +
...@@ -50,7 +50,7 @@ describe('find config', () => { ...@@ -50,7 +50,7 @@ describe('find config', () => {
(imageImportsEnabled (imageImportsEnabled
? '/// <reference types="next/image-types/global" />' + eol ? '/// <reference types="next/image-types/global" />' + eol
: '') + : '') +
'/// <reference path="./.next/types/routes.d.ts" />' + `import "./.next/types/routes.d.ts";` +
eol + eol +
eol + eol +
'// NOTE: This file should not be edited' + '// NOTE: This file should not be edited' +
...@@ -78,7 +78,7 @@ describe('find config', () => { ...@@ -78,7 +78,7 @@ describe('find config', () => {
(imageImportsEnabled (imageImportsEnabled
? '/// <reference types="next/image-types/global" />' + eol ? '/// <reference types="next/image-types/global" />' + eol
: '') + : '') +
'/// <reference path="./.next/types/routes.d.ts" />' + `import "./.next/types/routes.d.ts";` +
eol + eol +
eol + eol +
'// NOTE: This file should not be edited' + '// NOTE: This file should not be edited' +
......
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