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, // If migration to Nitro is complete, set to true
},
})
tsconfig.json
更新
如果您正在使用 TypeScript,您可以编辑您的 tsconfig.json
以受益于自动生成的 Nuxt 类型
tsconfig.json
{
+ "extends": "./.nuxt/tsconfig.json",
"compilerOptions": {
...
}
}
由于
.nuxt/tsconfig.json
是自动生成的,并且没有提交到版本控制中,因此您需要在运行测试之前生成该文件。在测试之前添加 nuxi prepare
作为一步,否则您会看到 TS5083: 无法读取文件 '~/.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
。