TypeScript 4.2 tunes tuple types

Now available as a production release, TypeScript 4.2 loosens restrictions on rest elements in tuple types and improves type alias preservation.

TypeScript 4.2 tunes tuple types
andy.brandon50 (CC BY-SA 2.0)

Microsoft has published the production release of TypeScript 4.2, an update to the popular open source language that adds types to JavaScript. Arrived February 23, TypeScript 4.2 features enhancements pertaining to tuple types and type aliases.

TypeScript 4.2 expands the ways rest elements in tuple types can be used. Previously, TypeScript only permitted rest elements in the last position of a tuple type. Now, rest elements can occur almost anywhere within a tuple, with a few restrictions. A rest element cannot be followed by another optional element or rest element, and only one rest element is permitted per tuple.

TypeScript 4.2 also includes these changes and enhancements:

  • With smarter type alias preservation, internals are smarter. Type construction is tracked. The language also tracks type aliases to instances of other aliases. The ability to print back types based on how they are used means avoiding some overly large types being displayed, which can translate to better .d.ts file output, error messages, and in-editor type displays in quick info and signature help.
  • Stricter checks are featured for the in operator. With JavaScript, using a non-object type on the side of the in operator results in a runtime error. TypeScript 4.2 ensures this can be caught at design time.
  • An abstract modifier can be specified on constructor signatures.
  • An --explainFiles flag helps developers understand why a file is in a program.
  • Rules have been relaxed between optional properties and string index signatures.
  • Type arguments will be parsed in a more spec-compliant manner. A breaking change, this could impact developers who use TypeScript’s API to parse type constructs in JavaScript files.
  • The in operator no longer allows primitive types on the right side. This also is a breaking change.
  • Improved uncalled function checks are featured in logical expressions, now applying within && and | | expressions.
  • A feature in the beta, in which template literal strings would either be given template string types or simplify to multiple string literal types, has been removed, after feedback.

TypeScript 4.2 can be installed via NuGet or via NPM using the following command:

npm install typescript

Copyright © 2021 IDG Communications, Inc.