> ## Documentation Index
> Fetch the complete documentation index at: https://bun.zhcndoc.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 为 Bun 安装 TypeScript 声明文件

要在项目中安装 Bun 内置 API 的 TypeScript 定义，请安装 `@types/bun`。

```sh terminal icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
bun add -d @types/bun # 开发依赖
```

***

以下是 Bun 项目推荐的完整 `compilerOptions` 配置。有了这个 `tsconfig.json`，你可以使用顶层 await，带扩展名或无扩展名的导入，以及 JSX。

```json tsconfig.json icon="file-json" theme={"theme":{"light":"github-light","dark":"dracula"}}
{
  "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-6)。

***

有关 Bun 中 TypeScript 支持的完整指南，请参阅 [生态系统 > TypeScript](/runtime/typescript)。
