要安装 Bun 内置 API 的 TypeScript 类型定义,请安装 @types/bun。
bun add -d @types/bun # dev dependency此时,您应该能够在 TypeScript 文件中引用 Bun 全局变量,而不会在编辑器中看到错误。
console.log(Bun.version);
建议的 compilerOptions
Bun 支持诸如 top-level await、JSX 和扩展名 .ts 导入等功能,而 TypeScript 默认不允许这些功能。下面是一组推荐的 Bun 项目 compilerOptions,这样您就可以使用这些功能而不会看到 TypeScript 的编译器警告。
{
"compilerOptions": {
// Environment setup & latest features
"lib": ["ESNext"],
"target": "ESNext",
"module": "Preserve",
"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,
"noUncheckedIndexedAccess": true,
"noImplicitOverride": true,
// Some stricter flags (disabled by default)
"noUnusedLocals": false,
"noUnusedParameters": false,
"noPropertyAccessFromIndexSignature": false,
},
}
如果您在一个新目录中运行 bun init,它将为您生成此 tsconfig.json。(更严格的标志默认禁用。)
bun init