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
Bun object in your TypeScript files without seeing errors in your editor.
What is @types/bun?
What is @types/bun?
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
Bunnamespace with all runtime APIs - Type definitions for
bun:*built-in modules likebun:test,bun:sqlite,bun:ffi - Augmented types for Node.js compatibility APIs
- Web API types that extend standard TypeScript lib types
Recommended tsconfig.json
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
bun init in a fresh directory, this tsconfig.json will be auto-generated for you.
terminal
Key compiler options explained
module: Preserve
module: Preserve
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.moduleResolution: bundler
moduleResolution: bundler
Uses the
bundler resolution algorithm, which matches the behavior of modern bundlers and runtimes like Bun. This enables features like:- Importing TypeScript files with
.tsextensions - Proper resolution of
exportsfield inpackage.json - Support for extensionless imports
allowImportingTsExtensions: true
allowImportingTsExtensions: true
Allows importing TypeScript files with Without this flag, TypeScript would require you to omit the extension.
.ts and .tsx extensions:verbatimModuleSyntax: true
verbatimModuleSyntax: true
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.noEmit: true
noEmit: true
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
Useawait at the top level of your modules:
JSX and TSX
Bun can execute.jsx and .tsx files directly:
terminal
Extensioned imports
Import TypeScript files with their full extensions:Decorators
Bun supports experimental decorators and the TypeScript 5.0+ decorator proposal:tsconfig.json
Path mapping
Bun respectspaths in tsconfig.json for module resolution:
tsconfig.json
Type checking
Bun’s runtime doesn’t perform type checking—it only strips type annotations. For type checking, use the TypeScript compiler:terminal
package.json:
package.json
terminal
DOM types
If you’re building a browser application and need DOM types, add"DOM" to the lib array:
tsconfig.json
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
@types/node can conflict with Bun’s types. Bun’s types take precedence for built-in modules.
Transpilation options
Configure how Bun transpiles TypeScript viabunfig.toml:
bunfig.toml
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 typeandexport typestatements- Type-only imports/exports
Enums
TypeScript enums are converted to JavaScript objects: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-serverorcoc-tsserver - Sublime Text: Install the TypeScript plugin
- Emacs: Use
tideorlsp-modewithtypescript-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:Update tsconfig.json
Use the recommended configuration above.
Performance
Bun’s TypeScript transpiler is significantly faster thantsc 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
.tsfiles 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.