Node.js adds experimental TypeScript support, as it ‘simply cannot be ignored’

Node.js adds experimental TypeScript support, as it ‘simply cannot be ignored’
party

The Node.js project has added TypeScript support – behind an “experimental” flag – with Technical Steering Committee member Marco Ippolito stating that the feature is “crucial to move the ecosystem forward, it has been requested on all the surveys, and it simply cannot be ignored.”

According to Ippolito, commenting on a pull request that was merged yesterday, “we must acknowledge users want to run node foo.ts without installing external dependencies or loaders.”

The experimental flag is --experimental-strip-types and, as the name suggests, it removes types from the code and transforms the TypeScript into JavaScript.

There are limitations. Only the types are stripped, and other TypeScript features such as enums and namespaces are not supported, and settings in the TypeScript configuration file tsconfig.json are ignored. Source maps – used to debug JavaScript when it is minified or otherwise mangled for deployment – are not generated, though by replacing inline types with white space line numbers are preserved. TypeScript files in the node_modules directory, used by dependencies, are not supported, in order to “discourage package authors from publishing packages written in TypeScript.”

That may sound half-hearted, but it is deliberately intended as “lightweight support,” according to the documentation. Those wanting full support are asked to use a third-party package such as tsx.

TypeScript, which as its name implies is a strongly typed language, compiles to JavaScript and is popular for applications which run either in the browser or on a JavaScript runtime such as the Google-sponsored V8, used by Node.js. Surveys typically place TypeScript among the top ten programming languages, with the Redmonk programming language survey commenting earlier this year that it has “slow but steady growth as it moves up one spot from seven to six.” JavaScript’s dynamic nature can make it more error-prone and insecure, whereas the TypeScript compiler detects more coding mistakes before deployment – but only if type-checking is performed, which the lightweight support coming to Node.js does not do.

Node.js was created by Ryan Dahl as a JavaScript runtime and first released in 2009. It remains far and away the most popular JavaScript runtime, despite Dahl himself creating a second project called Deno – which he said fixes “design mistakes in Node.” Deno uses TypeScript as its primary language though, and has made some impact. As has bun – another runtime which “treats TypeScript as a first class citizen” though it does not type-check the code. Deno, by contrast, can type-check TypeScript but does not do so by default for performance reasons.

The consequence of the above is that built-in TypeScript support for Node.js will be convenient, but should be used alongside other tools – such as TypeScript’s own compiler – to get the type-checking and other features that make TypeScript worthwhile.

Another issue noted by developers is that new versions of TypeScript may get out of step with Node.js – especially if staying with the long-term support versions of Node.js.

The roadmap though allows for more features to be added in future, and Ippolito remarked that the list of TypeScript features that require transformation outside Node.js is expected to “change in the future.”

Despite limitations, this is a key feature coming to Node.js that will help to keep it current. Enthused another developer: “My favorite Deno feature is coming to node directly. Awesome!”