Node.js — Running TypeScript Natively

Date:

Share:

Since v23.6.0, Node.js enables “type stripping” by default. If you are using v23.6.0 or later and your source code contains only erasable typescript syntax, you do not need this article.

Since V22.6.0, Node.js has experimental support for some TypeScript syntax via “type stripping”. You can write code that’s valid TypeScript directly in Node.js without the need to transpile it first.

The --experimental-strip-types flag tells Node.js to strip the type annotations from the TypeScript code before running it.

node --experimental-strip-types example.ts

And that’s it! You can now run TypeScript code directly in Node.js without the need to transpile it first, and use TypeScript to catch type-related errors.

In V22.7.0 this experimental support was extended to transform TypeScript-only syntax, like enums and namespace, with the addition of the --experimental-transform-types flag. Enabling --experimental-transform-types automatically implies that --experimental-strip-types is enabled, so there’s no need to use both flags in the same command:

node --experimental-transform-types another-example.ts

From v23.6.0 onwards, type stripping is enabled by default (you can disable it via --no-experimental-strip-types), enabling you to run any supported syntax, so running files like the one below with node file.ts is supported:

function foo(bar: number): string {
  return 'hello';
}

However, running any code that requires transformations, like the code below still needs the use of --experimental-transform-types:

enum MyEnum {
  A,
  B,
}

console.log(MyEnum.A);

Future versions of Node.js will include support for TypeScript without the need for a command line flag.

At the time of writing, the experimental support for TypeScript in Node.js has some limitations.

You can get more information on the API docs.

Configuration

The Node.js TypeScript loader (Amaro) does not need or use tsconfig.json to run TypeScript code.

We recommend configuring your editor and tsc to reflect Node.js behavior by creating a tsconfig.json using the compilerOptions listed here, as well as using TypeScript version 5.7 or higher.

Thanks to all the contributors who have made this feature possible. We hope that this feature will be stable and available in the LTS version of Node.js soon.

We can understand that this feature is experimental and has some limitations; if that doesn’t suit your use-case, please use something else, or contribute a fix. Bug reports are also welcome, please keep in mind the project is run by volunteers, without warranty of any kind, so please be patient if you can’t contribute the fix yourself.

Source link

Subscribe to our magazine

━ more like this

Orcas Bringing Humans Gifts Of Food

Wild orcas have been observed intentionally offering humans pieces of fish, marine mammals, and even seaweed across 34 recorded instances, suggesting a rare form...

Sali Hughes on beauty: I’ve got a real problem with neck creams. Here’s why … | Life and style

If you’ve ever kindly searched for a recommendation of a neck cream from me, then you’ll know there are virtually none on record. This is...

Guava Nails Are The Juiciest Summer Manicure Trend

The most popular iteration is a cat-eye finish: “The colors are layered using ombré techniques, jelly finishes, or magnetic cat-eye effects to mimic the...

Apple Watch Notifications Are About to Get Less Annoying

I've had an Apple Watch for nearly a...

Is AI Just Software? – Feld Thoughts

I decided to ask Claude (Opus 4), Gemini (2.5 Pro), and ChatGPT a few personal questions. I’m tired of the excessive anthropomorphizing of “AI...