Bun Setup
This guide covers using typesugar with Bun.
Current Status
Bun has its own TypeScript transpiler that doesn't support TypeScript transformer plugins. This means typesugar macros don't work with Bun's native transpiler.
However, you can still use typesugar with Bun by using a build step.
Workaround: Pre-compile with tsc/Vite
Option 1: Use typesugar CLI for development
bash
# Build with typesugar
bunx typesugar build
# Run the compiled output
bun dist/index.jsOption 2: Use Vite for bundling
typescript
// vite.config.ts
import { defineConfig } from "vite";
import typesugar from "unplugin-typesugar/vite";
export default defineConfig({
plugins: [
typesugar({
// Typecheck expanded output (since Vite doesn't typecheck)
strict: true,
}),
],
build: {
target: "esnext",
outDir: "dist",
lib: {
entry: "src/index.ts",
formats: ["es"],
},
},
});bash
# Build with Vite
bun run vite build
# Run with Bun
bun dist/index.jsProject Setup
package.json
json
{
"scripts": {
"build": "typesugar build",
"dev": "typesugar watch",
"start": "bun dist/index.js"
},
"devDependencies": {
"@typesugar/transformer": "^0.1.0",
"ts-patch": "^3.0.0",
"typescript": "^5.0.0"
}
}tsconfig.json
json
{
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "Bundler",
"outDir": "./dist",
"plugins": [{ "transform": "@typesugar/transformer", "type": "program" }]
}
}Testing with Bun
For tests, you have two options:
Option 1: Pre-compile test files
bash
# Build tests first
typesugar build --project tsconfig.test.json
# Run compiled tests with Bun
bun test dist/Option 2: Use Vitest
Vitest works with Bun and supports the typesugar plugin:
typescript
// vitest.config.ts
import { defineConfig } from "vitest/config";
import typesugar from "unplugin-typesugar/vite";
export default defineConfig({
plugins: [typesugar()],
test: {
include: ["src/**/*.test.ts"],
},
});bash
bunx vitestFuture Support
Bun has an open issue for supporting TypeScript transformer plugins. When this lands, typesugar will work natively with Bun's transpiler.
In the meantime, the build-step approach works reliably and maintains full type safety.
Troubleshooting
"comptime is not a function"
This error means Bun ran your TypeScript directly without macro expansion. Use the build step as described above.
Slow builds
For faster iteration:
- Use
typesugar watchfor incremental builds - Only rebuild changed files
- Consider using Vite's dev server for web projects
Next Steps
- Vitest Setup for testing
- Editor Setup
