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, // 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