All Articles

Node.js Now Supports TypeScript By Default

Matt Pocock
Matt PocockMatt is a well-regarded TypeScript expert known for his ability to demystify complex TypeScript concepts.

Node 23 will soon be able to run TypeScript files without any extra configuration.

Marco Ippolito, who has been driving TypeScript support in Node for the last year, landed a PR unflagging --experimental-strip-types in Node 23.

Practically, this means a few things:

  • You can create an index.ts file containing TS syntax, like type annotations.
  • You can run node index.ts with no further flags
  • Node will strip out the types using a version of swc, then run the resulting code.

Let's treat this article as a FAQ:

How Can I Try It?

It's now available in the Node Nightly version, as mentioned by Marco. In a few days it'll likely get a release in Node 23.

Will Node Typecheck My Files?

No. Node will not typecheck your files when it runs them. This is a good thing - it means that typechecking can run in a separate process to running your app.

You'll need to run tsc --watch locally to typecheck your app as you work.

What Should Be In My tsconfig.json?

This is a tailored version of my TSConfig Cheat Sheet.

{
  "compilerOptions": {
    /* Base Options: */
    "esModuleInterop": true,
    "skipLibCheck": true,
    "target": "es2022",
    "allowJs": true,
    "resolveJsonModule": true,
    "moduleDetection": "force",
    "isolatedModules": true,
    "verbatimModuleSyntax": true,
    /* Strictness */
    "strict": true,
    "noUncheckedIndexedAccess": true,
    "noImplicitOverride": true,
    /* Node Stuff */
    "allowImportingTsExtensions": true,
    "module": "NodeNext",
    "noEmit": true,
    /* If your code doesn't run in the DOM: */
    "lib": [
      "es2022"
    ],
  }
}

This will likely change in TS 5.8, where the TypeScript team are planning to ship a compatibility flag. Not sure what's in it yet, but it'll make interop with Node's TS support easier.

Are Enums and Namespaces Supported?

Enums and namespaces are not supported by default. Check out the other features which are not supported here. If you want support for other TS features, you can add --experimental-transform-types to make it work.

I'm personally not too sad about this - especially about the enums.

Note that runtime namespaces are not supported, but type-only namespaces (using declare namespace) are supported. This is nice - I like type-only namespaces.

Do I Need To Transpile To JavaScript For Production?

Node apps

If you're deploying your code to a serverless platform, yes. Transpiling and minifying your code will be key to getting a good cold start.

If you're deploying to a place where cold starts matter less, then probably not! There will be a small cost when the app initially loads your TypeScript (and I do mean small), but once the process is running it's unlikely to cause slowdowns.

One possible wrinkle is if your code dynamically loads other TypeScript files during its processing. I could see that resulting in a little extra slowdown - so tread carefully.

Libraries

Yes, you still need to transpile your TypeScript to JavaScript before shipping it to NPM. There are two reasons for this:

  • So JavaScript users can still consume your library
  • TypeScript works faster when consuming declaration files (.d.ts files), so not shipping them means you're making your consumers' TS experience slower.

Monorepos

If you're creating a library in a monorepo, for only that monorepo to consume, I still recommend transpiling your TypeScript files to JavaScript.

The reason is the same as for libraries - the more .d.ts files you have in your monorepo, the faster your TS experience will be.

Will This Come To Previous Versions Of Node?

Yes, TypeScript support will be coming to Node 22, but not Node 20.

Marco mentioned this on X.

Matt's signature

Node.js Now Supports TypeScript By Default