Microsoft has published a beta version of TypeScript 4.2, an update to the popular open source language that adds types to JavaScript. With a final release due February 23, TypeScript 4.2 features enhancements pertaining to tuple types and type aliases.
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
string
through 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
in
operator. With JavaScript, using a non-object type on the side of the incode 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.
The TypeScript 4.2 beta can be installed via NuGet or via NPM using the following command:
npm install typescript@beta