You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在不启用NextJS TypeScript编译的情况下获取next-env.d.ts以实现智能提示?

不启用TypeScript编译生成next-env.d.ts的方法
  • 临时生成next-env.d.ts文件

    1. 在项目根目录创建一个基础的tsconfig.json文件,内容如下:
      {
        "compilerOptions": {
          "target": "es5",
          "lib": ["dom", "dom.iterable", "esnext"],
          "allowJs": true,
          "skipLibCheck": true,
          "strict": false,
          "forceConsistentCasingInFileNames": true,
          "noEmit": true,
          "incremental": true,
          "esModuleInterop": true,
          "module": "esnext",
          "moduleResolution": "node",
          "resolveJsonModule": true,
          "isolatedModules": true,
          "jsx": "preserve"
        },
        "include": ["next-env.d.ts", "**/*.js", "**/*.jsx"],
        "exclude": ["node_modules"]
      }
      
    2. 运行一次next dev或next build命令,Next.js会自动在项目根目录生成next-env.d.ts文件
    3. 文件生成完成后,删除或重命名tsconfig.json(比如改为tsconfig.temp.json),这样Next.js就不会切换到TypeScript编译模式
  • 配置JS项目的类型提示

    1. 保留生成的next-env.d.ts文件,在JS文件中可以通过JSDoc引用Next.js的类型,示例:
      /** @type {import('next').NextPage} */
      export default function Home() {
        return <div>Hello World</div>;
      }
      
    2. 可选:创建jsconfig.json文件优化编辑器的类型识别能力:
      {
        "compilerOptions": {
          "checkJs": true,
          "types": ["next"]
        },
        "include": ["**/*.js", "**/*.jsx", "next-env.d.ts"]
      }
      

内容的提问来源于stack exchange,提问作者Michael Landis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 06:36:28