Skip to content
tezvyn:

Variadic Tuple Types: Type-Safe Spreads for Tuples

Source: typescriptlang.orgHardHow cards are made

Variadic Tuple Types: Type-Safe Spreads for Tuples

Variadic tuple types let you use spread syntax (...) inside tuple type definitions, just like you do with array values. This is crucial for typing functions that manipulate tuples, like concat, without writing endless overloads or losing type information.

Why it exists

Before TypeScript 4.0, there was no good way to type functions that took tuples and returned a new, transformed tuple. A function like concat would require an unmanageable number of overloads to handle different input lengths, a situation dubbed "death by a thousand overloads". The only alternative, a generic array type like Array<T|U>, lost all information about element order and length.

The mental model

Think of variadic tuple types as applying JavaScript's spread syntax (...) to the type level. Just as [...arr1, ...arr2] creates a new array value, [...T, ...U] creates a new tuple type by composing the types from the source tuples T and U. It allows types to represent dynamic patterns of elements, not just a fixed list.

How it works

Variadic tuple types allow the spread element syntax (...T) to be used inside a tuple type definition, where T is a generic type constrained to be a tuple or array. This lets you create types that are composed of the elements of other types. For example, a concat function can be typed as function concat<T extends any[], U extends any[]>(arr1: T, arr2: U): [...T, ...U]. The return type [...T, ...U] dynamically constructs a new, precise tuple type by spreading the element types from the inputs.

When to use it

Use variadic tuple types when writing generic functions that operate on and return tuples, especially when you need to preserve the exact sequence and types of elements. This is common in functional programming utilities (like pipe or curry), state management selectors, or any API that needs to precisely model argument lists and their transformations.

When not to use it

Avoid variadic tuples when you're working with simple, homogeneous arrays where the exact number and order of elements don't need to be tracked at the type level. If string[] or Array<User> is a sufficient description of your data, the added complexity of variadic tuples is unnecessary.

One canonical example

Typing a function to get the tail of a tuple (all elements except the first) was previously impossible to do generically. With variadic tuples, it's elegant: function tail<T extends any[]>(arr: readonly [any, ...T]): T { const [_, ...rest] = arr; return rest; }. If you call this with [1, 'hello', true], TypeScript correctly infers the return type as ['hello', true], not a generic (string | boolean)[].

Interview question

When are variadic tuple types most advantageous in TypeScript?

  • a.When simplifying the type definition for homogeneous arrays where element order is not critical.
  • b.When defining a tuple type with a fixed, predetermined number of elements and specific types.
  • c.When creating generic functions that transform tuples and must maintain precise element types and their order in the output.Correct
  • d.When you need to define an array that can hold elements of various types without enforcing a specific order.
Why?

Variadic tuple types are crucial for generic functions that manipulate tuples, allowing them to preserve the exact sequence and types of elements in the output, avoiding the 'death by a thousand overloads' problem. Option D describes a generic array type like Array<T|U>, which variadic tuples improve upon by adding type and order precision.

Just read this? Test yourself on what you have been reading.

Read the original → typescriptlang.org

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on typescript — each one lists the topics its interview covers.

See open roles