Bun foundations
Create and run a TypeScript project
Initialize a Bun project, inspect its files, and run TypeScript directly without a separate build step.
Let’s create the project we’ll use throughout the course.
Create a directory and initialize Bun inside it:
mkdir bun-notes
cd bun-notes
bun init --yes
bun init creates a small TypeScript project. The --yes flag accepts every default so it doesn’t ask questions. The exact files may change as Bun evolves, but you should see package.json, tsconfig.json, and index.ts, plus a .gitignore and a README.md.
Open package.json. It’s a normal npm-style manifest, with "type": "module" set so we can use import everywhere. Nothing Bun-specific is required to get started.
Now replace the contents of index.ts with this:
const message: string = 'Hello from Bun'
console.log(message)
Run the file:
bun index.ts
Bun prints:
Hello from Bun
Notice what we did not do. We did not run tsc first, and we did not create a JavaScript copy of the file. Bun transpiles TypeScript while it loads the file. That means it strips the type annotations so the engine can run what’s left.
Transpiling is not type checking
This is the part that trips people up. Bun removes the types, it does not verify them.
Try it. Change the file to assign a number to a string:
const message: string = 42
console.log(message)
Run bun index.ts again. It prints 42 and exits happily. The type error is still there, Bun just didn’t look for it.
So keep your editor’s TypeScript checks enabled. And for a command-line check you can run in CI, install TypeScript and run the compiler without emitting files:
bun add --dev typescript
bunx tsc --noEmit
With the broken file in place, tsc reports the problem:
index.ts:1:25 - error TS2322: Type 'number' is not assignable to type 'string'.
Put the string back before moving on.
Running code and checking types are two different jobs. A successful bun index.ts tells you the program ran. It tells you nothing about the types. Keep both commands in your workflow and you’ll never confuse the two.
Lesson completed