This Crazy Syntax Lets You Get An Array Element's Type
Learn how to extract the type of an array element in TypeScript using the powerful Array[number]
trick.
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:
index.ts
file containing TS syntax, like type annotations.node index.ts
with no further flagsswc
, then run the resulting code.Let's treat this article as a FAQ:
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.
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.
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.
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.
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.
Yes, you still need to transpile your TypeScript to JavaScript before shipping it to NPM. There are two reasons for this:
.d.ts
files), so not shipping them means you're making your consumers' TS experience slower.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.
Yes, TypeScript support will be coming to Node 22, but not Node 20.
Marco mentioned this on X.
Node.js Now Supports TypeScript By Default
Learn how to extract the type of an array element in TypeScript using the powerful Array[number]
trick.
Learn how to publish a package to npm with a complete setup including, TypeScript, Prettier, Vitest, GitHub Actions, and versioning with Changesets.
Enums in TypeScript can be confusing, with differences between numeric and string enums causing unexpected behaviors.
Is TypeScript just a linter? No, but yes.
It's a massive ship day. We're launching a free TypeScript book, new course, giveaway, price cut, and sale.
Learn why the order you specify object properties in TypeScript matters and how it can affect type inference in your functions.