Files
bun.sh/docs/typescript.md
Risu c34bbb2e3f fix: organize tsconfig (#8654)
* fix: organize tsconfig

* fix: remove unnecessary comments in tsconfig

* docs: revert changes on comments and md text

* docs: fix case
2024-02-16 22:19:31 -08:00

1.4 KiB

To install the TypeScript definitions for Bun's built-in APIs, install @types/bun.

$ bun add -d @types/bun # dev dependency

At this point, you should be able to reference the Bun global in your TypeScript files without seeing errors in your editor.

console.log(Bun.version);

Suggested compilerOptions

Bun supports things like top-level await, JSX, and extensioned .ts imports, which TypeScript doesn't allow by default. Below is a set of recommended compilerOptions for a Bun project, so you can use these features without seeing compiler warnings from TypeScript.

{
  "compilerOptions": {
    // Enable latest features
    "lib": ["ESNext"],
    "target": "ESNext",
    "module": "ESNext",
    "moduleDetection": "force",
    "jsx": "react-jsx", // support JSX
    "allowJs": true, // allow importing `.js` from `.ts`

    // 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
  }
}

If you run bun init in a new directory, this tsconfig.json will be generated for you.

$ bun init