TypeScript 4.7 adds ESM support in Node.js

Now available in a release candidate, the latest 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 release candidate (RC) stage, offers ECMAScript module (ESM) support for Node.js 16 as well as a multitude of coding enhancements.

The RC was unveiled May 11. TypeScript 4.7 is the latest planned version of Microsoft’s strongly typed JavaScript. The TypeScript 4.7 beta introduced April 8 backed ECMAScript module support in Node.js 12, something that had been planned for TypeScript 4.5 late last year but was delayed.

However, because Node.js 12 is no longer supported, TypeScript’s builders have started the stable target at Node.js 16, which should provide newer ES module functionality such as pattern trailers while also defaulting TypeScript to a higher target that supports top-level await.

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 become generally available on May 24. The RC can be accessed via NuGet or NPM:

npm install typescript@rc

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.
  • Organize Imports is performed in a group-aware manner.
  • Snippet completions are provided for object literal methods.
  • A readonly tuple now will treat its length property as readonly. This is a breaking change.
  • A preview editor command is featured for Go to Source Definition.
  • In another breaking change, when writing a …spread in JSX, TypeScript now enforces stricter checks that the given type is actually an object. As a result, values with the types unknown and never, and, more rarely, null and undefined, no longer can be spread into JSX elements.

Following the beta, TypeScript’s builders found that a typeof capability on #private fields, which had been planned for the release, had issues with API compatibility. The team also began doubting whether typeof this.#somePrivate composes well on declaration emit. As a result, this capability has been left out of TypeScript 4.7.

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

Copyright © 2022 IDG Communications, Inc.

How to choose a low-code development platform