tsconfig.json

了解 Nuxt 如何管理项目中不同部分的 TypeScript 配置。

Nuxt 自动生成 多个 TypeScript 配置文件(.nuxt/tsconfig.app.json.nuxt/tsconfig.server.json.nuxt/tsconfig.node.json.nuxt/tsconfig.shared.json),其中包含项目推荐的基本 TypeScript 配置,引用了自动导入API 路由类型、路径别名等。

您的 Nuxt 项目根目录应包含以下 tsconfig.json 文件:

tsconfig.json
{
  "files": [],
  "references": [
    {
      "path": "./.nuxt/tsconfig.app.json"
    },
    {
      "path": "./.nuxt/tsconfig.server.json"
    },
    {
      "path": "./.nuxt/tsconfig.shared.json"
    },
    {
      "path": "./.nuxt/tsconfig.node.json"
    }
  ]
}
我们不建议直接修改此文件的内容,因为这样可能会覆盖 Nuxt 或其他模块依赖的重要设置。相反,应通过 nuxt.config.ts 文件进行扩展。
在此处了解有关 Nuxt 项目不同类型上下文的更多信息。

扩展 TypeScript 配置

您可以在 nuxt.config.ts 文件中为每个上下文(appsharednodeserver)自定义 Nuxt 项目的 TypeScript 配置。

nuxt.config.ts
export default defineNuxtConfig({
  typescript: {
    // 自定义 tsconfig.app.json
    tsConfig: {
      // ...
    },
    // 自定义 tsconfig.shared.json
    sharedTsConfig: {
      // ...
    },
    // 自定义 tsconfig.node.json
    nodeTsConfig: {
      // ...
    },
  },
  nitro: {
    typescript: {
      // 自定义 tsconfig.server.json
      tsConfig: {
        // ...
      },
    },
  },
})