為 Bun 安裝 TypeScript 宣告檔案
要為專案新增 Bun 內建 API 的 TypeScript 型別定義,請安裝 @types/bun。
terminal
bun add -d @types/bun # 開發相依
以下是 Bun 專案推薦使用的完整 compilerOptions 設定。藉助此 tsconfig.json,你可以使用頂層 await、帶副檔名或不帶副檔名的匯入,以及 JSX。
tsconfig.json
{
"compilerOptions": {
// 環境設定與最新特性
"lib": ["ESNext"],
"target": "ESNext",
"module": "Preserve",
"moduleDetection": "force",
"jsx": "react-jsx",
"allowJs": true,
"types": ["bun"],
// 打包模式
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"noEmit": true,
// 最佳實踐
"strict": true,
"skipLibCheck": true,
"noFallthroughCasesInSwitch": true,
"noUncheckedIndexedAccess": true,
"noImplicitOverride": true,
// 一些更嚴格的選項(預設關閉)
"noUnusedLocals": false,
"noUnusedParameters": false,
"noPropertyAccessFromIndexSignature": false
}
}
如果你使用的是 TypeScript 6.0 或更高版本,還需要在 compilerOptions 中新增 "types": ["bun"]。詳情請參閱 TypeScript 6 和 7。
請參閱 TypeScript。