Skip to main content
Bun natively executes TypeScript files without requiring a separate compilation step. Type annotations are stripped at runtime using Bun’s fast transpiler.

Quick Start

Run TypeScript files directly:
Run TypeScript
No tsc, no ts-node, no configuration needed.

Features

Zero Config

Works out of the box without tsconfig.json

Fast Transpilation

Written in Zig for maximum performance

Source Maps

Accurate source maps for debugging

Path Mapping

Respects tsconfig paths automatically

How It Works

Bun’s TypeScript transpiler:
  1. Strips type annotations - Removes TypeScript syntax
  2. Preserves runtime behavior - No type checking or validation
  3. Generates source maps - Maps back to original TypeScript
  4. Caches output - Hashes input to avoid re-transpiling
Bun does not perform type checking. Use tsc --noEmit for type validation.

Supported TypeScript Features

Type Annotations

All type annotations are removed:
Type annotations

Enums

Enums are compiled to JavaScript objects:
Enums
Output behavior:
  • Regular enums → JavaScript objects
  • Const enums → Inlined values (optimized)

Namespaces

Namespaces are supported but discouraged (use ES modules):
Namespaces

Decorators

Bun supports both experimental and standard decorators:
Decorators
Configuration:
tsconfig.json
Implementation: src/runtime.zig:203-204

Using Declarations

Explicit resource management (TC39 Stage 3):
Using declarations
Compiles to try/finally blocks with resource disposal.

Import/Export Extensions

Bun supports TypeScript import extensions:
Import extensions

tsconfig.json Support

Bun respects tsconfig.json configuration:

Path Mapping

tsconfig.json
Using path aliases
Implementation: src/resolver/resolver.zig

Compiler Options

Bun honors these tsconfig.json options:
  • moduleResolution - “node”, “bundler”, “node16”, “nodenext”
  • baseUrl - Base directory for module resolution
  • paths - Path mapping for imports
  • types - Type declaration packages to include
  • typeRoots - Directories containing type definitions
  • jsx - “react”, “react-jsx”, “react-jsxdev”, “preserve”
  • jsxFactory - JSX factory function (default: React.createElement)
  • jsxFragmentFactory - Fragment factory (default: React.Fragment)
  • jsxImportSource - Import source for automatic runtime
  • experimentalDecorators - Enable legacy decorators
  • emitDecoratorMetadata - Emit design-time metadata
  • useDefineForClassFields - Class field initialization
  • importsNotUsedAsValues - Preserve/remove imports
  • esModuleInterop - Enable default import interop
  • allowSyntheticDefaultImports - Allow default imports from CommonJS
  • resolveJsonModule - Allow importing .json files
Bun ignores type-checking options (strict, noImplicitAny, etc.) as it doesn’t perform type checking.

Runtime Transpiler Cache

Bun caches transpiled TypeScript to avoid repeated parsing: Cache location:
  • Linux: ~/.bun/install/cache/
  • macOS: ~/Library/Caches/Bun/
  • Windows: %LOCALAPPDATA%\Bun\Cache\
Cache invalidation:
  • File content changes (hash-based)
  • tsconfig.json modifications
  • Bun version updates
Implementation: src/runtime.zig:210 - Runtime transpiler cache

Disable Cache

Disable cache

Type Checking

Bun focuses on execution, not type checking. For type validation:

Using TypeScript Compiler

Type checking

Package.json Scripts

package.json

IDE Integration

Recommended setup:
  1. VS Code - Install TypeScript extension
  2. Configure workspace:
    .vscode/settings.json

CommonJS & ESM Interop

Bun seamlessly handles TypeScript with both module systems:

ES Modules (.ts, .mts)

ESM TypeScript

CommonJS (.cts)

CommonJS TypeScript

Type Imports

Type-only imports

Declaration Files (.d.ts)

Bun recognizes TypeScript declaration files:
Declaration files

Debugging TypeScript

Bun generates source maps for accurate debugging:

VS Code Launch Config

.vscode/launch.json
See Debugger documentation for more details.

Performance

Bun’s TypeScript transpiler is significantly faster than tsc or ts-node:
  • Written in Zig: Native code, no JavaScript overhead
  • Parallel parsing: Multi-threaded for large projects
  • Lazy transpilation: Only transpiles imported files
  • Efficient caching: Hash-based cache invalidation
Benchmark comparison (transpiling 10,000 TypeScript files):
Benchmarks approximate, actual performance depends on code complexity.

Implementation Details

TypeScript transpilation implementation:
  • Parser: src/js_parser.zig - Parses TypeScript AST
  • Printer: src/js_printer.zig - Generates JavaScript
  • Transpiler: src/transpiler.zig - Orchestrates parsing/printing
  • Runtime features: src/runtime.zig:144-327 - Feature flags and configuration

Limitations

Bun’s TypeScript support has some limitations:
  • No type checking (use tsc --noEmit)
  • No .d.ts generation (use tsc --emitDeclarationOnly)
  • Some advanced features may not work identically to tsc

Troubleshooting

Module Resolution Issues

Check resolution

Path Mapping Not Working

Ensure tsconfig.json is in the project root: