TExcelTAD
Home › Node.js › TypeScript in Node

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.

ts
// 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:

node · try it yourself
// 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
bash · try it yourself
$ node greet.ts
Hi Kim, you are 31
Hi Tom, you are 27
Hi 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:

ts
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:

ts
import { greet } from './greet.ts';          // ✅
import type { User } from './types.ts';      // ✅ type-only import is erased
import { greet } from './greet';             // ❌ ERR_MODULE_NOT_FOUND

Whether 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:

json
{
  "compilerOptions": {
    "target": "esnext",
    "module": "nodenext",
    "noEmit": true,
    "allowImportingTsExtensions": true,
    "erasableSyntaxOnly": true,
    "verbatimModuleSyntax": true,
    "strict": true
  }
}
json
{
  "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:

ts · try it yourself
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 .js plus .d.ts files — Node refuses to strip types from files inside node_modules.

Test yourself

What does Node's type stripping do with type annotations?

  1. Checks them and stops on errors
  2. Replaces them with whitespace and runs the rest
  3. Converts them into runtime checks
  4. Sends them to tsc

Which of these does NOT work with plain type stripping?

  1. interface User { name: string }
  2. let n: number = 1
  3. enum Color { Red, Green }
  4. import type { X } from './x.ts'

How should you import a local TypeScript file when running it directly with Node?

  1. import { a } from './a'
  2. import { a } from './a.js'
  3. import { a } from './a.ts'
  4. require('./a')