Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News TypeScript 4.0 Adds Long-Awaited Variadic Tuple Types

TypeScript 4.0 Adds Long-Awaited Variadic Tuple Types

This item in japanese

The TypeScript team announced the release of TypeScript 4.0, which includes long-awaited variadic tuple type support and other improvements without introducing any major breaking changes.

For years, TypeScript lead architect Anders Hjelsberg has talked about the challenges of adding support for variadic kinds. For tuple types, it's easy to quickly run into challenges where typing something as simple as the concatenation of two array or tuple types to make a new array quickly leads to highly verbose type overloads.

TypeScript program manager Daniel Rosenwasser explains:

TypeScript 4.0 brings two fundamental changes, along with inference improvements, to make typing these possible.

The first change is that spreads in tuple type syntax can now be generic. This means that we can represent higher-order operations on tuples and arrays even when we don’t know the actual types we’re operating over. When generic spreads are instantiated (or, replaced with a real type) in these tuple types, they can produce other sets of array and tuple types. The second change is that rest elements can occur anywhere in a tuple – not just at the end!

Together these changes take a case of "death by a thousand overloads" into a relatively simple function signature:

type Arr = readonly any[];

function concat<T extends Arr, U extends Arr>(arr1: T, arr2: U): [...T, ...U] {
    return [...arr1, ...arr2];

Beyond this example, the type inference process for rest parameters and rest type elements eases support for partially applying arguments. Variadic tuple types generally provide significant improvements for function composition.

Improvements to tuple types and parameter lists also allow strong type validation for other common JavaScript practices that were previously challenging to type. These include manipulating argument lists and passing them to other functions.

Another significant addition in TypeScript 4.0 is class property inference from constructors. TypeScript's control flow analysis determines the types of properties in classes where the noImplicitAny flag is enabled.

TypeScript 4.0 supports a new ECMAScript feature with three new assignment operators: &&=, ||=, and ??=. These operators support compound assignment (e.g. the more familiar +=) for logical and nullish coalescing operators, filling in a missing gap in JavaScript and TypeScript.

If these operators get used with getters or setters containing side effects, these operators only perform assignments if necessary.

Try/Catch statements also get an improvement in TypeScript 4.0. Previously catch clause variables were almost always typed as any. Now, they should be typed more accurately as unknown, reminding developers that type checks should occur before operating on these values.

TypeScript 4.0 improves build mode performance time when using the --noEmitOnError flag following incremental builds. Before 4.0, information from a previous incremental compilation was not cached, resulting in slower compile times. Similarly, incremental builds can now leverage the --noEmit flag.

With editors supporting TypeScript Language Services, a new refactoring option now exists to convert common patterns to now leverage optional chaining and nullish coalescing, recent TC39 features added in TypeScript 3.7.

For very large codebases, TypeScript users would previously experience slow startup times as the TypeScript compiler would work through a codebase. With TypeScript 4.0 in Visual Studio Code there is support for partial semantic mode at startup, which provides a much briefer startup delay.

TypeScript 4.0 introduces only a few minor breaking changes. Changes to the standard lib.d.ts declarations include the removal of document.origin which was deprecated by browser standards in favor of self.origin. Other breaking changes include properties overriding accessors now always issues and error, and operands for the delete operator when using StrictNullChecks must be any, unknown, never, or be optional.

Finally, the TypeScript Node factory functions for producing AST Nodes has a new node factory API replacing the previous API.

See the TypeScript 4.0 release blog for more details on these breaking changes.

TypeScript continues to gain popularity, with over 50 million monthly downloads via npm and Stack Overflow survey participants voting TypeScript as the 2nd most-loved language.

Work is already underway for TypeScript 4.1, expected in November 2020. Some of the TypeScript 4.1 release goals include recursive conditional types, key augmentation in mapped types, ES module interoperability within Node.js, the recent TC39 export * as default addition, and perhaps pulling type information directly from WebAssembly modules.

TypeScript users are encouraged to attend the first online TSConf on October 9th, 2020, which will include sessions by members of the TypeScript team and the greater TypeScript community.

TypeScript is open source software available under the Apache 2 license. Contributions and feedback are encouraged via the TypeScript GitHub project and should follow the TypeScript contribution guidelines and Microsoft open-source code of conduct.

Rate this Article