1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
Bun can directly execute `.ts` and `.tsx` files just like vanilla JavaScript, with no extra configuration. If you import a `.ts` or `.tsx` file (or an `npm` module that exports these files), Bun internally transpiles it into JavaScript then executes the file.
{% callout %}
**Note** — Similar to other build tools, Bun does not typecheck the files. Use [`tsc --noEmit`](https://www.typescriptlang.org/docs/handbook/compiler-options.html) (the official TypeScript CLI) if you're looking to catch static type errors.
{% /callout %}
## Type definitions
To install TypeScript definitions for Bun's built-in APIs, first install `bun-types`.
```sh
$ bun add -d bun-types # dev dependency
```
Then include `"bun-types"` in the `compilerOptions.types` in your `tsconfig.json`:
```json-diff
{
"compilerOptions": {
+ "types": ["bun-types"]
}
}
```
## Compiler options
Bun's runtime implements [modern ECMAScript features](https://github.com/sudheerj/ECMAScript-features), like bigint literals, nullish coalescing, dynamic imports, `import.meta`, `globalThis`, ES modules, top-level await, and more. To use these features without seeing TypeScript errors in your IDE, your `tsconfig.json` needs to be properly configured.
These are the recommended `compilerOptions` for a Bun project.
> The config below sets `moduleResolution` to `bundler` which requires TypeScript option is set to `"bundler"` to support [path mapping](#path-mapping).
```jsonc
{
"compilerOptions": {
// enable latest features
"lib": ["esnext"],
"module": "esnext",
"target": "esnext",
// requires typescript 5.x+
// use "nodenext" for earlier versions
"moduleResolution": "bundler",
// support JSX, CommonJS
"jsx": "react-jsx", // support JSX (value doesn't matter)
"allowJs": true, // allow importing `.js` from `.ts`
"esModuleInterop": true, // allow default imports for CommonJS modules
// best practices
"strict": true,
"forceConsistentCasingInFileNames": true,
"skipLibCheck": true,
// add Bun type definitions
"types": ["bun-types"]
}
}
```
If you use `bun init`, an appropriate `tsconfig.json` is automatically generated for you.
## Path mapping
When resolving modules, Bun's runtime respects path mappings defined in [`compilerOptions.paths`](https://www.typescriptlang.org/tsconfig#paths) in your `tsconfig.json`. No other runtime does this.
Given the following `tsconfig.json`...
```json
{
"compilerOptions": {
"paths": {
"data": ["./data.ts"]
}
}
}
```
...the import from `"data"` will work as expected.
{% codetabs %}
```ts#index.ts
import { foo } from "data";
console.log(foo); // => "Hello world!"
```
```ts#data.ts
export const foo = "Hello world!"
```
{% /codetabs %}
|