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