Skip to main content

Overview

Bun is designed with TypeScript in mind. It natively executes TypeScript and TSX files without requiring a separate transpilation step. This makes Bun an ideal runtime for TypeScript projects.
terminal
Bun’s transpiler only strips TypeScript syntax—it doesn’t perform type checking. Use tsc (the official TypeScript compiler) for type checking in development and CI.

Install type definitions

To install TypeScript type definitions for Bun’s built-in APIs, install @types/bun:
terminal
At this point, you should be able to reference the global Bun object in your TypeScript files without seeing errors in your editor.
The @types/bun package is a shim package that re-exports types from bun-types, which lives in the Bun repository under packages/bun-types.The types include:
  • The global Bun namespace with all runtime APIs
  • Type definitions for bun:* built-in modules like bun:test, bun:sqlite, bun:ffi
  • Augmented types for Node.js compatibility APIs
  • Web API types that extend standard TypeScript lib types

Bun supports features like top-level await, JSX, and extensioned imports (.ts imports) that TypeScript doesn’t allow by default. Here’s a recommended tsconfig.json for Bun projects that enables these features without compilation warnings:
tsconfig.json
If you run bun init in a fresh directory, this tsconfig.json will be auto-generated for you.
terminal

Key compiler options explained

Sets the module system to Preserve, which keeps import/export statements as-is without transforming them. This is ideal for Bun since it natively supports ESM.
Uses the bundler resolution algorithm, which matches the behavior of modern bundlers and runtimes like Bun. This enables features like:
  • Importing TypeScript files with .ts extensions
  • Proper resolution of exports field in package.json
  • Support for extensionless imports
Allows importing TypeScript files with .ts and .tsx extensions:
Without this flag, TypeScript would require you to omit the extension.
Ensures that import/export statements are preserved exactly as written. This helps catch errors where you might accidentally use import type syntax that would be stripped.
Prevents TypeScript from generating output files. Since Bun handles transpilation at runtime, you don’t need TypeScript to emit JavaScript files.

Native TypeScript features

Bun supports several TypeScript features natively:

Top-level await

Use await at the top level of your modules:
index.ts

JSX and TSX

Bun can execute .jsx and .tsx files directly:
Component.tsx
terminal

Extensioned imports

Import TypeScript files with their full extensions:
This is more explicit and aligns with browser-native ESM behavior.

Decorators

Bun supports experimental decorators and the TypeScript 5.0+ decorator proposal:
tsconfig.json

Path mapping

Bun respects paths in tsconfig.json for module resolution:
tsconfig.json
Now you can use these path aliases:

Type checking

Bun’s runtime doesn’t perform type checking—it only strips type annotations. For type checking, use the TypeScript compiler:
terminal
Add this as a script in your package.json:
package.json
Then run:
terminal

DOM types

If you’re building a browser application and need DOM types, add "DOM" to the lib array:
tsconfig.json
This enables types for document, window, HTMLElement, and other browser APIs.

Node.js types

Bun implements many Node.js APIs for compatibility. If you’re using Node.js modules, you may want to install Node.js type definitions:
terminal
However, be aware that @types/node can conflict with Bun’s types. Bun’s types take precedence for built-in modules.

Transpilation options

Configure how Bun transpiles TypeScript via bunfig.toml:
bunfig.toml
Alternatively, set these options in tsconfig.json and Bun will respect them:
tsconfig.json

Type stripping

Bun strips the following TypeScript syntax during transpilation:
  • Type annotations
  • Interface declarations
  • Type aliases
  • Enums (converted to JavaScript objects)
  • Namespace declarations (experimental)
  • import type and export type statements
  • Type-only imports/exports

Enums

TypeScript enums are converted to JavaScript objects:
Becomes:

Editor support

VS Code

Install the Bun for Visual Studio Code extension for enhanced Bun support:
  • Syntax highlighting for bunfig.toml
  • Debug support for Bun
  • Run configurations for bun run, bun test, etc.

Other editors

Most editors with TypeScript support work great with Bun:
  • IntelliJ IDEA / WebStorm: Full TypeScript support built-in
  • Vim/Neovim: Use typescript-language-server or coc-tsserver
  • Sublime Text: Install the TypeScript plugin
  • Emacs: Use tide or lsp-mode with typescript-language-server

Compatibility with TypeScript versions

Bun’s transpiler targets compatibility with the latest stable TypeScript release. Bun generally supports:
  • ✅ TypeScript 5.0+ (full support)
  • ✅ TypeScript 4.x (full support)
  • ⚠️ TypeScript 3.x (mostly supported, but some features may not work)

Migration from Node.js

If you’re migrating a TypeScript project from Node.js to Bun:
1

Install Bun types

terminal
2

Update tsconfig.json

3

Remove ts-node or tsx

You no longer need these packages since Bun natively runs TypeScript:
terminal
4

Update scripts

Replace ts-node or tsx with bun in your package.json scripts:
package.json
5

Test your application

terminal

Performance

Bun’s TypeScript transpiler is significantly faster than tsc or ts-node:
  • Startup time: Bun starts 4x faster than Node.js with ts-node
  • Transpilation: Bun’s transpiler is written in Zig and optimized for speed
  • No compilation step: Run .ts files directly without a build step
For production builds, consider using bun build to bundle and minify your TypeScript code.

Additional resources

Runtime TypeScript

Learn more about TypeScript support in Bun’s runtime.

Transpiler API

Use Bun’s transpiler programmatically in your code.

Bundler

Bundle TypeScript for production with Bun.build.

Testing

Write TypeScript tests with Bun’s test runner.