Web Developer

|

Stockholm, Sweden

Delivering scalable solutions for
startups, enterprises, and agencies.

Tuples

What is a Tuple?

A tuple is a fixed-size array where you know exactly what type each element should be and in what order. Think of it as a structured list with specific slots for specific types.

Tuple vs Array syntax

  • Tuple: [string, number, boolean] - exactly 3 elements in that exact order
  • Array: string[] - any number of string elements
A tuple is like a form with specific fields: "Name (text), Age (number), Active (yes/no)". An array is like a list where you can add as many items of the same type as you want.

Key benefits

  • Type safety - Each position has a guaranteed type, preventing mix-ups
  • Predictable structure - Always the same number of elements in the same order
  • Clear intentions - Code explicitly shows what data structure to expect
  • Better tooling - IDE knows exactly what properties and methods are available

When to use tuples

  • Coordinates - [number, number] for x, y positions
  • API responses - [boolean, string] for success status and message
  • Function returns - [data, error] pattern
  • Configuration pairs - [string, number] for key-value settings

Practical example

Instead of getUserData(): any[] returning unpredictable data, use getUserData(): [string, number, boolean] to guarantee you'll get name, age, and active status in that exact order.

Remember

  • Tuples have fixed length - you can't add or remove elements
  • Each position has a specific type - TypeScript enforces this
  • Order matters - [string, number] is different from [number, string]

Tuples bring structure and predictability to your data, making your code safer and more maintainable.

Web Developer
Harry Yates 2025®