---
title: "tsconfig.json"
description: "了解 Nuxt 如何管理项目中不同部分的 TypeScript 配置。"
canonical_url: "https://nuxt.zhcndoc.com/docs/4.x/directory-structure/tsconfig"
---
# tsconfig.json

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

Nuxt [会自动生成](https://nuxt.zhcndoc.com/docs/4.x/guide/concepts/typescript#auto-generated-types) 多个 TypeScript 配置文件（`.nuxt/tsconfig.app.json`、`.nuxt/tsconfig.server.json`、`.nuxt/tsconfig.node.json` 和 `.nuxt/tsconfig.shared.json`），其中包含项目推荐的基础 TypeScript 配置、对 [自动导入](https://nuxt.zhcndoc.com/docs/4.x/guide/concepts/auto-imports)、[API 路由类型](https://nuxt.zhcndoc.com/docs/4.x/guide/concepts/server-engine#typed-api-routes)、路径别名等的引用。

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

```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"
    }
  ]
}
```

::warning
我们不建议直接修改此文件的内容，因为这样可能会覆盖 Nuxt 或其他模块依赖的重要设置。相反，应通过 `nuxt.config.ts` 文件进行扩展。
::

::read-more{to="https://nuxt.zhcndoc.com/docs/4.x/guide/concepts/typescript#project-references"}
在这里详细了解 Nuxt 项目的不同类型上下文。
::

## 扩展 TypeScript 配置

你可以在 `nuxt.config.ts` 文件中自定义 Nuxt 项目的 TypeScript 配置：使用 `typescript.tsConfig` 一次性为每个上下文设置共享的 `compilerOptions`，并分别为每个上下文（`app`、`shared`、`node` 和 `server`）进行覆盖。

<!-- @case-police-ignore tsConfig -->

```ts twoslash [nuxt.config.ts]
// @errors: 2353
export default defineNuxtConfig({
  typescript: {
    // shared compiler options for every generated tsconfig
    tsConfig: {
      compilerOptions: {
        // ...
      },
    },
    // customize tsconfig.app.json
    appTsConfig: {
      // ...
    },
    // 自定义 tsconfig.shared.json
    sharedTsConfig: {
      // ...
    },
    // 自定义 tsconfig.node.json
    nodeTsConfig: {
      // ...
    },
    // customize tsconfig.server.json
    serverTsConfig: {
      // ...
    },
  },
})
```

::note
在 `typescript.tsConfig` 中设置的大多数 `compilerOptions` 都会与每个上下文共享，但也有少数例外。特定于 DOM 和 Vue 的选项（例如 `lib`、`jsx` 和 `jsxImportSource`）仅适用于应用程序代码，因此它们只会应用于 `tsconfig.app.json`。Nuxt 还会按上下文管理 `types`、`paths` 和 `noEmit`（`node`、`shared` 和 `server` 配置会有意不生成任何内容，也不会扫描环境类型），因此在 `typescript.tsConfig` 中设置这些选项不会改变它们。需要覆盖这些选项时，请使用相应的上下文选项（`appTsConfig`、`nodeTsConfig`、`sharedTsConfig` 或 `serverTsConfig`）。
::

::note
`typescript.serverTsConfig` 会扩展 `tsconfig.server.json`。`nitro.typescript.tsConfig` 与其保持同步，但已弃用，因此请改用 `typescript.serverTsConfig`。
::


## Sitemap

See the full [sitemap](https://nuxt.zhcndoc.com/sitemap.md) for all pages.
