> ## Documentation Index
> Fetch the complete documentation index at: https://bun-1dd33a4e-farm-ad2450b3-transpiler-cache-version-namespa.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# TypeScript

> Using TypeScript with Bun, including type definitions and compiler options

Install `@types/bun` to get TypeScript definitions for Bun's built-in APIs.

```sh theme={"theme":{"light":"github-light","dark":"dracula"}}
bun add -d @types/bun # dev dependency
```

You can now reference the `Bun` global in your TypeScript files without seeing errors in your editor.

```ts theme={"theme":{"light":"github-light","dark":"dracula"}}
console.log(Bun.version);
```

## Suggested `compilerOptions`

Bun supports top-level await, JSX, and imports with `.ts` extensions, which TypeScript doesn't allow by default. The following `compilerOptions` are recommended for a Bun project, so you can use these features without compiler warnings from TypeScript.

```jsonc theme={"theme":{"light":"github-light","dark":"dracula"}}
{
  "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,
  },
}
```

Running `bun init` in a new directory generates this `tsconfig.json` for you. (The stricter flags are disabled by default.)

```sh theme={"theme":{"light":"github-light","dark":"dracula"}}
bun init
```
