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 项目,我们建议使用 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