初面网初面网

Bun 对 Typescript 的支持

获取Bun内置API的TypeScript定义

bun add -d @types/bun # dev dependency

这样,您可以在TypeScript文件中引用Bun全局变量,且编辑器不会显示错误提示。

设置 tsconfig.json

使用下面命令生成json文件

bun init
{
  "compilerOptions": {
    // Environment setup & latest features
    "lib": ["ESNext"],
    "target": "ESNext",
    "module": "Preserve",
    "moduleDetection": "force",
    "jsx": "react-jsx",
    "allowJs": true,
    "types": ["bun"],

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

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

    // Some stricter flags (disabled by default)
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "noPropertyAccessFromIndexSignature": false
  }
}

Typescript 6 or 7

由于6和7版本不再自动识别bun,所以,如果用的是 Typescript 6 或以上,还需要额外的配置

{
  "compilerOptions": {
    // 其他属性
    "types": ["bun"]
  }
}

更新于 2026/7/16