Chapter 7. Variadic Tuple Types

Tuple types are arrays with a fixed length and where every type of each element is defined. Tuples are heavily used in libraries like React as it’s easy to destructure and name elements, but outside of React they also have gained recognition as a nice alternative to objects.

A variadic tuple type is a tuple type that has the same properties—defined length and the type of each element is known—but where the exact shape is yet to be defined. They basically tell the type system that there will be some elements, but we don’t know yet which ones they will be. They are generic and meant to be substituted with real types.

What sounds like a fairly boring feature is much more exciting when we understand that tuple types can also be used to describe function signatures, as tuples can be spread out to function calls as arguments. This means we can use variadic tuple types to get the most information out of functions and function calls, and functions that accept functions as parameters.

This chapter provides a lot of use cases on how we can use variadic tuple types to describe several scenarios where we use functions as parameters and need to get the most information from them. Without variadic tuple types, these scenarios would be hard to develop or outright impossible. After reading through, you will see variadic tuple types as a key feature for functional programming patterns.

7.1 Typing a concat Function

Problem

You have a concat function that takes ...

Get TypeScript Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.