TypeScript in Node
TypeScript adds static types to JavaScript: you describe the shape of your data, and the compiler warns you about mistakes before the code runs. Most new Node projects are written in TypeScript today. For a long time that meant a build step (tsc) or a helper like ts-node/tsx, but modern Node can run many .ts files directly.
Type stripping: how Node runs .ts files
Node does not type-check your code. Instead it uses type stripping: it removes the type annotations — replacing them with spaces so line and column numbers stay the same — and runs the JavaScript that is left.
// greet.ts
interface User {
name: string;
age: number;
}
function greet(user: User): string {
return `Hi ${user.name}, you are ${user.age}`;
}
const users: User[] = [{ name: 'Kim', age: 31 }, { name: 'Tom', age: 27 }];
for (const u of users) console.log(greet(u));After stripping, Node effectively runs this — the same program with the types blanked out:
// greet.ts after type stripping (annotations replaced by whitespace)
function greet(user ) {
return `Hi ${user.name}, you are ${user.age}`;
}
const users = [{ name: 'Kim', age: 31 }, { name: 'Tom', age: 27 }];
for (const u of users) console.log(greet(u));Hi Kim, you are 31
Hi Tom, you are 27
| Node version | Support |
|---|---|
| 22.6 | node --experimental-strip-types app.ts |
| 22.7 | --experimental-transform-types also handles enum and namespace |
| 23.6 / 22.18 | type stripping is on by default — just node app.ts |
| recent 24.x / 25.2+ | no more "experimental" warning; Node 25.2 marks type stripping stable |
$ node greet.ts
Hi Kim, you are 31
Hi Tom, you are 27Hi Kim, you are 31
Hi Tom, you are 27
What type stripping can't do
Because Node only erases types, any TypeScript feature that produces runtime code is rejected unless you enable transformation:
| Feature | Works with plain stripping? |
|---|---|
type, interface, annotations, generics, as, satisfies |
✅ yes — they disappear |
import type { X } from './x.ts' |
✅ yes |
enum Color { Red, Green } |
❌ no — generates an object |
namespace Utils { … } with values |
❌ no |
constructor parameter properties (constructor(private x: number)) |
❌ no |
tsconfig.json paths aliases |
❌ ignored by Node |
The rule of thumb: if you can delete the type syntax and get valid JavaScript, it works. Instead of an enum, use a plain object with as const:
const Color = { Red: 'red', Green: 'green' } as const;
type Color = (typeof Color)[keyof typeof Color]; // 'red' | 'green'TypeScript 5.8 added the erasableSyntaxOnly compiler option, which makes tsc report exactly the constructs Node can't strip.
Imports need real file extensions
Node resolves files exactly as written, so imports of local TypeScript files must use the .ts extension:
import { greet } from './greet.ts'; // ✅
import type { User } from './types.ts'; // ✅ type-only import is erased
import { greet } from './greet'; // ❌ ERR_MODULE_NOT_FOUNDWhether a .ts file is an ES module or CommonJS follows the usual rules ("type" in package.json); .mts and .cts force one or the other.
Type checking is still your job
Node happily runs code that has type errors — the types are simply removed. Keep tsc in your workflow (in your editor, in CI, or in a check script) as a type checker only:
{
"compilerOptions": {
"target": "esnext",
"module": "nodenext",
"noEmit": true,
"allowImportingTsExtensions": true,
"erasableSyntaxOnly": true,
"verbatimModuleSyntax": true,
"strict": true
}
}{
"type": "module",
"scripts": {
"start": "node src/server.ts",
"check": "tsc",
"test": "node --test"
},
"devDependencies": {
"typescript": "^5.9.0",
"@types/node": "^24.0.0"
}
}@types/node gives the editor type information for fs, http, process and all other built-ins.
Here is what the checker reports for a mistake that Node itself would not notice until it produced a wrong result. The ExcelTAD TypeScript playground runs the same checker, so you can try it right here:
interface Product {
name: string;
price: number;
}
function total(items: Product[]): number {
return items.reduce((sum, p) => sum + p.price, 0);
}
const cart: Product[] = [
{ name: 'Mouse', price: 25 },
{ name: 'Cable', price: '9' },
];
console.log(total(cart));main.ts(12,20): error TS2322: Type 'string' is not assignable to type 'number'.
Fix the price to the number 9 and run again — the checker is happy and the program prints 34.
Other options
| Tool | When to use it |
|---|---|
tsc (compile to .js) |
publishing a library, or targeting older Node versions |
tsx (npm) |
runs any TypeScript, incl. enums and path aliases, with zero config |
| bundlers (esbuild, Vite, tsup) | building apps/libraries into optimised bundles |
Tip: Libraries on npm should still publish compiled
.jsplus.d.tsfiles — Node refuses to strip types from files insidenode_modules.
Test yourself
What does Node's type stripping do with type annotations?
- Checks them and stops on errors
- Replaces them with whitespace and runs the rest
- Converts them into runtime checks
- Sends them to
tsc
Which of these does NOT work with plain type stripping?
interface User { name: string }let n: number = 1enum Color { Red, Green }import type { X } from './x.ts'
How should you import a local TypeScript file when running it directly with Node?
import { a } from './a'import { a } from './a.js'import { a } from './a.ts'require('./a')