BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News TypeScript 4.7 and 4.8 Beta Releases Add ESM for Node.js and Better Type Inference

TypeScript 4.7 and 4.8 Beta Releases 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 introduces ES Module (ESM) support for Node.js, improved type inference and control flow analysis, and significant performance improvements.

Since ES6 introduced modules in 2015, work has been underway to move the JavaScript and TypeScript ecosystems to the native module format. Early usage was primarily limited to authoring, with build tools such as Webpack and transpilers such as TypeScript converting code to modules that would run in various environments.

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

Daniel Rosenwasser, TypeScript Program Manager, explains:

For the last 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 built on a different module system called CommonJS (CJS). Interoperating between the two brings large challenges, with many new features to juggle.

TypeScript 4.7 adds two new module settings: node16 and nodenext. Through the use of package.json's "type": "module", Node.js determines whether .js files are interpreted as ESM or CommonJS modules. ESM supports key features include import/export statements and top-level async/await.

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

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

These releases add more than just Node.js ESM support. Control-Flow Analysis for bracketed element access helps narrow the types of element accesses when the indexed keys are literal types and unique symbols. The --strictPropertyInitialization flag now checks that computed properties get initialized before the end of a constructor body.

TypeScript 4.7 also supports more granular type inference from functions within objects and arrays. New support for instantiation expressions allow for the narrowing of generics during instantiation.

TypeScript 4.8 adds many correctness and consistency improvements to the --strictNullChecks mode. Improvements to intersection and union types help TypeScript narrow its type definitions.

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

TypeScript transpiler improvements with --build, --watch, and --incremental reduce typical transpilation 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-late August, in time for TypeScript turning 10 years old in October!

TypeScript is open-source software available under the Apache 2 license. Contributions and feedback are encouraged via the TypeScript GitHub project and should follow the TypeScript contribution guidelines and Microsoft open-source code of conduct.

About the Author

Rate this Article

Adoption
Style

BT