TypeScript

学习如何在Nuxt Bridge中使用TypeScript。

移除模块

  • 移除 @nuxt/typescript-build:Bridge已启用相同功能
  • 移除 @nuxt/typescript-runtimenuxt-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/tsconfig.json 的所有选项都将被您在 tsconfig.json 中定义的选项覆盖。 如果您使用自己的配置覆盖选项,例如 "compilerOptions.paths",将导致TypeScript无法考虑 ./.nuxt/tsconfig.json 中的模块解析。这可能会导致诸如 #imports 等模块解析不被识别。如果您需要进一步扩展 ./.nuxt/tsconfig.json 提供的选项,可以在 nuxt.config 中使用 alias 属性。nuxi 会自动识别并相应扩展 ./.nuxt/tsconfig.json