TypeScript 4.7 adds ESM support in Node.js

Now in a beta release stage, the next version of Microsoft’s strongly typed JavaScript introduces support for ECMAScript modules in Node.js.

TypeScript 4.7 adds ESM support in Node.js

Getty Images

TypeScript 4.7, now in a beta release, offers ECMAScript module (ESM) support for Node.js 12 as well as a multitude of coding enhancements.

Unveiled April 8, TypeScript 4.7 is the latest planned version of Microsoft’s strongly typed JavaScript. TypeScript 4.7 backs ECMAScript module support in Node.js 12, something that had been planned for TypeScript 4.5 late last year but was delayed.

Nightly-only support for ECMAScript modules in Node.js was rolled out in the TypeScript 4.5 timeframe. These modules enable reuse of code via packaging. Implementing this support has been difficult because Node.js was built on a different module system, CommonJS. TypeScript 4.7 adds this functionality with two module settings, node12 and nodenext. Feedback on this capability is sought and can be offered on GitHub.

TypeScript 4.7 is due to reach a release candidate stage on May 10, followed by a final release on May 24. The beta can be accessed via NPM:

npm install typescript@beta

Other capabilities in TypeScript 4.7 include:

  • To control module detection, TypeScript 4.7 introduces the option moduleDetection.
  • A moduleSuffixes option is supported to customize lookup of module specifiers.
  • With control flow analysis for computer properties, TypeScript analyzes the type of computed properties and narrows them correctly.
  • TypeScript now can perform more granular inferences from functions with objects and arrays. This allows types of these functions to consistently flow in a left-to-right manner, just like for plain arguments.
  • Functions and constructors can be fed type arguments directly.
  • Developers can explicitly specify variance on type parameters.
  • typeof queries can be performed on private fields.
  • Organize Imports is performed in a group-aware manner.
  • Snippet completions are provided for object literal methods.
  • Under strictNullChecks, the type checker disables a type safety hole that was maintained for backward compatibility, where type parameters were considered to be assignable to {} and object. This is a breaking change.
  • A readonly tuple now will treat its length property as readonly. This also is a breaking change.

Predecessor TypeScript 4.6, centering on capabilities such as ECMAScript 2022 support and recursion checks, was published February 28.

Paul Krill is an editor at large at InfoWorld, whose coverage focuses on application development.

Copyright © 2022 IDG Communications, Inc.