TypeScript
了解如何在 Nuxt Bridge 中使用 TypeScript。
移除模块
- 移除
@nuxt/typescript-build:Bridge 提供了相同的功能。 - 移除
@nuxt/typescript-runtime和nuxt-ts:Nuxt 2 已内置运行时支持。
设置 bridge.typescript
import { defineNuxtConfig } from '@nuxt/bridge'
export default defineNuxtConfig({
bridge: {
typescript: true,
nitro: false // 如果已完成 Nitro 迁移,设置为 true
}
})
更新 tsconfig.json
如果您使用 TypeScript,可以编辑您的 tsconfig.json 以利用 Nuxt 自动生成的类型:
tsconfig.json
{
+ "extends": "./.nuxt/tsconfig.json",
"compilerOptions": {
...
}
}
由于
.nuxt/tsconfig.json 是生成的文件且不会纳入版本控制,您需要在运行测试之前生成该文件。请在测试前添加 nuxi prepare 步骤,否则您可能会遇到 TS5083: Cannot read file '~/.nuxt/tsconfig.json' 错误。对于现代 Nuxt 项目,我们推荐使用 TypeScript 项目引用 而不是直接扩展 .nuxt/tsconfig.json。请注意,
.nuxt/tsconfig.json 中扩展的所有选项都将被您在 tsconfig.json 中定义的选项覆盖。
如果您用自己的配置覆盖选项(如 "compilerOptions.paths"),TypeScript 将不会考虑 .nuxt/tsconfig.json 中的模块解析。这可能导致 #imports 等模块解析不被识别。如果您需要进一步扩展 .nuxt/tsconfig.json 提供的选项,可以在 nuxt.config 中使用 alias 属性。nuxi 将会识别它们并相应地扩展 .nuxt/tsconfig.json。