Bun

TypeScript

若要安裝 Bun 內建 API 的 TypeScript 定義,請安裝 @types/bun

bun add -d @types/bun # dev dependency

在這個時候,您應該可以在 TypeScript 檔案中參照 Bun 全域變數,而不會在編輯器中看到錯誤。

console.log(Bun.version);

建議的 compilerOptions

Bun 支援頂層 await、JSX 和擴充的 .ts 匯入等功能,而 TypeScript 預設不允許這些功能。以下是 Bun 專案建議的 compilerOptions,這樣您就可以使用這些功能,而不會看到 TypeScript 的編譯器警告。

{
  "compilerOptions": {
    // Enable latest features
    "lib": ["ESNext"],
    "target": "ESNext",
    "module": "ESNext",
    "moduleDetection": "force",
    "jsx": "react-jsx",
    "allowJs": true,

    // Bundler mode
    "moduleResolution": "bundler",
    "allowImportingTsExtensions": true,
    "verbatimModuleSyntax": true,
    "noEmit": true,

    // Best practices
    "strict": true,
    "skipLibCheck": true,
    "noFallthroughCasesInSwitch": true,

    // Some stricter flags
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noPropertyAccessFromIndexSignature": true
  }
}

如果您在新的目錄中執行 bun init,這個 tsconfig.json 將會為您產生。(嚴格的標記預設為停用。)

bun init