TypeScript 4.2, launched January 12, 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.
- Template string expressions now always start with template literal types. Like string literal types, these types disappear and turn into
stringthrough a process known as widening, if one of the values is assigned to a mutable variable. Template literal types were introduced in TypeScript 4.1, the current production release. These types can model specific patterns of strings.
- Stricter checks are featured for the
abstractmodifier can be specified on constructor signatures.
--explainFilesflag helps developers understand why a file is in a program.
- Rules have been relaxed between optional properties and string index signatures.
inoperator no longer allows primitive types on the right side. This also is a breaking change.
The TypeScript 4.2 beta can be installed via NuGet or via NPM using the following command:
npm install typescript@beta