TypeScript 4.7 and 4.8 betas add ESM for Node.js and better type inference

The TypeScript team announced the release of TypeScript 4.8 beta and TypeScript 4.7, which introduce ES Module (ESM) support for Node.js, improved type inference and control flow parsing, and significant performance improvements .

Since ES6 introduced modules in 2015, work has been done to move the JavaScript and TypeScript ecosystems to the native module format. Early use was mostly limited to authoring, with authoring tools like Webpack and transpilers like TypeScript turning code into modules that would run in various environments.

As the module format has improved over the past few years, browsers natively support ESM loading, and now Node.js 16 does as well. TypeScript version 4.7 helps us move closer to a world where all JavaScript is built and used as ESM.

TypeScript Program Manager Daniel Rosenwasser explains:

Over the past few years, Node.js has been working to support ECMAScript Modules (ESM). This has been a very difficult feature since the Node.js ecosystem is based on a different module system called CommonJS (CJS). Interoperating between the two comes with big challenges, with lots of new features to juggle.

TypeScript 4.7 adds two new module parameters: node16 and nodenext. By using package.json’s “type”: “module”, Node.js determines whether .js files are interpreted as ESM or CommonJS modules. ESM supports key features such as import/export statements and top-level async/await.

Relative import paths with ESM need full file extensions in the path, and several techniques used by CommonJS modules are not supported, such as requirement and top-level module.

Node.js supports two new file extensions for modules always from ESM or CJS, .mjs and .cjs, so TypeScript has added analogs, .mts and .cts.

These releases add more than Node.js ESM support. Control flow analysis for parenthesized element accesses helps reduce element access types when the indexed keys are literal types and unique symbols. The –strictPropertyInitialization flag now checks that computed properties are initialized before the end of the constructor body.

TypeScript 4.7 also supports more granular type inference from functions inside objects and arrays. New support for instantiation expressions allows to reduce generics during instantiation.

TypeScript 4.8 adds many correction and consistency improvements to the –strictNullChecks mode. Improvements to intersection and union types help TypeScript constrain type definitions.

Also in TypeScript 4.8, the TypeScript compiler can better infer types within template string types.

TypeScript transpiler improvements with –build, –watch, and –incremental reduce typical transpiling times by 10-25%.

These two releases added dozens of other improvements and bug fixes. Read the full release notes to learn more about each release.

The official release of TypeScript 4.8 is expected in mid-August, in time for TypeScript to turn 10 in October!

TypeScript is open source software available under the Apache 2 License. Contributions and comments are encouraged via the TypeScript GitHub project and should follow the TypeScript Contribution Guidelines and the Microsoft Open Source Code of Conduct.

Leave a Comment

Your email address will not be published. Required fields are marked *