TypeScript 4.7 crosses the finish line

The latest version of Microsoft’s strongly typed JavaScript introduces support for ECMAScript modules in Node.js, adds coding enhancements.

TypeScript 4.7 crosses the finish line
Thinkstock

TypeScript 4.7 became generally available as a production release on May 24. The latest version of Microsoft’s strongly typed JavaScript, TypeScript 4.7 arrives with ECMAScript module (ESM) support for Node.js 16 as well as a multitude of coding enhancements.

ESM support for Node.js 16 was a late addition to the release. The TypeScript 4.7 beta introduced April 8 included ESM support for Node.js 12, which 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 started the stable target at Node.js 16. This not only provides newer ECMAScript module functionality such as pattern trailers, but also defaults 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, node16 and nodenext. Feedback on this capability is sought and can be offered on GitHub.

TypeScript 4.7 can be downloaded via NuGet or NPM:

npm install -D typescript

Other capabilities in TypeScript 4.7 include the following:

  • Control flow analysis for bracketed element access now narrows the types of element accesses when the indexed keys are literal types and unique symbols
  • To control module detection, TypeScript 4.7 introduces the option moduleDetection.
  • A moduleSuffixes option is supported to customize lookup of module specifiers.
  • 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.

A release candidate for TypeScript 4.7 was published on May 11. The previous version, TypeScript 4.6, which brought capabilities such as ECMAScript 2022 support and recursion checks, was published February 28.

Copyright © 2022 IDG Communications, Inc.