tsconfig.json
了解 Nuxt 如何管理项目中不同部分的 TypeScript 配置。
Maschinelle Übersetzung ·0 % geprüft
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 文件进行扩展。扩展 TypeScript 配置
你可以在 nuxt.config.ts 文件中自定义 Nuxt 项目的 TypeScript 配置:使用 typescript.tsConfig 一次性为每个上下文设置共享的 compilerOptions,并分别为每个上下文(app、shared、node 和 server)进行覆盖。
// @errors: 2353
export default defineNuxtConfig({
typescript: {
// gemeinsame Compiler-Optionen für jede generierte tsconfig
tsConfig: {
compilerOptions: {
// ...
},
},
// tsconfig.app.json anpassen
appTsConfig: {
// ...
},
// 自定义 tsconfig.shared.json
sharedTsConfig: {
// ...
},
// 自定义 tsconfig.node.json
nodeTsConfig: {
// ...
},
// tsconfig.server.json anpassen
serverTsConfig: {
// ...
},
},
})在
typescript.tsConfig 中设置的大多数 compilerOptions 都会与每个上下文共享,但也有少数例外。特定于 DOM 和 Vue 的选项(例如 lib、jsx 和 jsxImportSource)仅适用于应用程序代码,因此它们只会应用于 tsconfig.app.json。Nuxt 还会按上下文管理 types、paths 和 noEmit(node、shared 和 server 配置会有意不生成任何内容,也不会扫描环境类型),因此在 typescript.tsConfig 中设置这些选项不会改变它们。需要覆盖这些选项时,请使用相应的上下文选项(appTsConfig、nodeTsConfig、sharedTsConfig 或 serverTsConfig)。typescript.serverTsConfig 会扩展 tsconfig.server.json。nitro.typescript.tsConfig 与其保持同步,但已弃用,因此请改用 typescript.serverTsConfig。Was this helpful?