TypeScript 4.6 adds control flow analysis, ES2022 support | InfoWorld .

TypeScript Coding



TypeScript 4.6, the new iteration of Microsoft’s strongly typed superset of JavaScript, is now available as a production release, with capabilities ranging from control flow analysis to improved recursion depth checks.

The production release, published February 28, followed a release candidate published February 11 and a beta release offered on January 21.

With control flow analysis for destructured discriminated unions, TypeScript now can narrow types based on a discriminant party. When destructuring individual properties into a const declaration, or when destructuring a parameter into variables that are never assigned to, TypeScript will check if the destructured type is a discriminated union. If so, TypeScript can narrow types of variables depending on checks of other variables.

Also new in TypeScript 4.6, TypeScript’s --target option now supports ES2022, meaning features like class fields now have a stable output target where they can be preserved. Additionally, new built-in functionality such as the at() method on arrays can be used with this new --target setting or with --lib es2022.

Among previously cited capabilities, one improvement relaxes coding restrictions by allowing code in constructors before super(). In JavaScript classes, it is mandatory to call super() prior to referring to this. TypeScript also enforced this but was a bit too strict, according to a bulletin on TypeScript 4.6. It had been an error to contain any code at the beginning of a constructor if its containing class had property initializers. This made it “cheap” to check that super() was called before this was referenced, but resulted in valid code being rejected. TypeScript 4.6 relaxes this check. It also permits other code to run before super() while ensuring that super() occurs at the top level before references to this.

TypeScript 4.6 can be accessed through NuGet or via NPM:

npm install typescript

Other new features and improvements in TypeScript 4.6:

  • With control flow analysis for dependent parameters, TypeScript can narrow parameters that depend on others, in some situations where TypeScript infers from a signature in a rest parameter.
  • Improved recursion depth checks offer better relationship checking for recursive types.
  • TypeScript now can infer to indexed access types that immediately index into a mapped object type.
  • The set of syntax and binding errors in JavaScript files has been expanded. These will be seen if developers open JavaScript files in an editor such as Visual Studio or Visual Studio Code, or if they run JavaScript code through the TypeScript compiler.
  • A trace analyzer tool has been published, to help teams running into build performance issues.
  • Object rest expressions now drop members that appear to be unspreadable or generic objects. This is a breaking change.

The predecessor TypeScript 4.5 release was published last November, with Promise improvements.

Copyright © 2022 IDG Communications, Inc.