Tuple Type in TypeScript

  • A tuple is an array with a fixed number of elements, where each element has a specific type. Unlike arrays, tuples allow you to store values of different types at specific positions.
  • To declare a tuple type, you use square brackets `[]` and specify the types of the elements in order, separated by commas. Here's an syntax:

    let tupleName: [type1, type2, type3, ...];

  • Example:

    let person: [string, number] = ["John", 30];

  • In this example:
  • The first element must be a string (name).
  • The second element must be a number (age).
Accessing Tuple Elements
  • You can access tuple elements just like array elements, using their indices.


    let tuple: [string, number, boolean] = ["hello", 42, true];

  • In the example above, `tuple` is declared as a tuple with three elements. The first element is a string, the second element is a number, and the third element is a boolean.
  • You can access individual elements of a tuple using zero-based indexing:


    console.log(tuple[0]); // Output: "hello"
    console.log(tuple[1]); // Output: 42
    console.log(tuple[2]); // Output: true

  • Tuple types can be useful when you want to represent a collection of values with different types, but the number of elements and their types are fixed and known.
Modifying Tuples
  • You can also modify the values in a tuple, but you need to ensure that the new values conform to the types defined for each position in the tuple.

    // Example:
    let person: [string, number] = ["John", 30];
    person[0] = "Alice";  // Valid: replacing a string
    person[1] = 35;       // Valid: replacing a number

    // Error: Type 'string' is not assignable to type 'number'
    person[1] = "thirty-five";


Tuple with Optional Elements
  • TypeScript supports optional elements in tuples, meaning that some elements may or may not be present. This is done using the ? symbol.


    let tuple: [string, number, boolean, number?] = ["hello", 42, true];
    tuple.push(78)
    console.log(tuple[0]); // Output: "hello"
    console.log(tuple[1]); // Output: 42
    console.log(tuple[2]); // Output: true
    console.log(tuple[3]); // Output: 78

  • It's worth noting that TypeScript also provides additional utility types for working with tuples, such as `readonly`, `Partial`, and `Pick`. These utility types can help you define more specific constraints and transformations on tuples.
Differences Between Arrays and Tuples

Feature

Array

Tuple

Type of elements

Homogeneous (all elements have the same type)

Heterogeneous (different types at specific positions)

Length

Can be of any length

Fixed number of elements

Index Access

Can access any index

Access based on predefined types

Example

let arr: number[] = [1, 2, 3];

let tuple: [string, number] = ["John", 30];

 
Why Use Arrays and Tuples in TypeScript?
  • Arrays: Useful when you need to store a list of items that are all of the same type.
  • Tuples: Helpful when you want to store a fixed number of elements with different types (like representing a person's name and age).

No comments:

Post a Comment

Primitive Types in TypeScript

In TypeScript, primitive types are the most basic data types, and they are the building blocks for handling data. They correspond to simple ...