> ## Documentation Index
> Fetch the complete documentation index at: https://bun.ll1025.cn/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={null}
bun add -d @types/bun # 开发依赖
```

***

以下是 Bun 项目推荐的完整 `compilerOptions`。使用此 `tsconfig.json`，你可以使用顶层 await、带扩展名或不带扩展名的导入，以及 JSX。

```json tsconfig.json icon="file-json" theme={null}
{
  "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)。

***

参见 [TypeScript](/runtime/typescript)。
