TypeScript 5.0 rebuilt to use ECMAScript modules

With TypeScript 5.0, the TypeScript development team has restructured the entire codebase to use ECMAScript modules, reducing package size and improving performance.

multi color neon number 5 top five
Getty Images

TypeScript 5.0, due from Microsoft as a production release on March 16, has been restructured around the use of ECMAScript modules, a major infrastructure change for the strongly typed JavaScript variant.

Users of TypeScript 5.0, which currently is in a release candidate stage, will need to run Node.js 12 at a minimum. In return, npm installs promise to execute a little faster and take up less space, with the typescript package sized reduced by roughly 46%, Microsoft said in a March 9 bulletin. TypeScript too will be faster, with build times cut by 10% to 25%. API consumers of TypeScript likely will be unaffected.

TypeScript will not be shipping its API as ES modules yet. A CommonJS-authored API still will be provided, and existing build scripts will still work.

Providing background on the transition, Microsoft said the current TypeScript codebase predates ECMAScript modules, which were standardized in 2015. Not knowing how compatible ES modules would be with other module systems such as CommonJS, and not seeing a huge benefit for authoring in modules at the time, TypeScript instead used namespaces, formerly called internal modules.

Although namespaces had useful features like scopes that could merge across files, making it easy to break up a project across files and expose it as a single variable, most modern JavaScript and TypeScript code is authored using modules. Thus, by using namespaces, TypeScript’s builders weren’t using TypeScript the way most of their users are—and missing out on that experience. Namespaces also brought runtime performance issues.

Moving to modules allows the builders of TypeScript to close the experience gap with their users, speed up the development of TypeScript, and make TypeScript faster, Microsoft said. However, while TypeScript is now written with modules, the actual JS files have not changed format. Libraries still act as CommonJS when executed in a CommonJS environment.

Copyright © 2023 IDG Communications, Inc.