ELEVATE YOUR BUSINESS WITH

Limitless customization options & Elementor compatibility let anyone create a beautiful website with Valiance.

Ts Tuples in TypeScript

SELECT * FROM `itio_tutorial_master` WHERE `tutorial_menu`='19' AND `tutorial_submenu`='1154' AND `tutorial_status`=1 LIMIT 1

Ts Tuples in TypeScript

A tuple in TypeScript is a type of array with a fixed number of elements, where each element has a specific type. Tuples are useful when you want to represent an array with a known length and type for each position.


Defining a Tuple

To define a tuple, use the square bracket [] notation with the types of its elements listed in order.

typescript

let person: [string, number];person = ["Alice", 30]; // Valid// person = [30, "Alice"]; // Error: Type 'number' is not assignable to type 'string'


Features of Tuples

  1. Fixed Length
    Tuples have a fixed number of elements. You cannot add or remove elements unless the tuple type allows it explicitly.

  2. Heterogeneous Types
    Each element can have a different type.


Accessing Tuple Elements

Access tuple elements using their index, just like an array.

typescript

let point: [number, number] = [10, 20];console.log(point[0]); // Output: 10console.log(point[1]); // Output: 20


Optional Tuple Elements

You can make elements optional using the ? operator.

typescript

let data: [string, number?];data = ["Alice"]; // Validdata = ["Alice", 42]; // Valid// data = [42]; // Error: Type 'number' is not assignable to type 'string'


Readonly Tuples

Make a tuple immutable using the readonly modifier.

typescript

let coordinates: readonly [number, number] = [10, 20];// coordinates[0] = 15; // Error: Cannot assign to '0' because it is a read-only property


Using Tuples with Destructuring

Tuples work seamlessly with destructuring.

typescript

let employee: [string, number] = ["Bob", 35];let [name, age] = employee;console.log(name); // Output: "Bob"console.log(age); // Output: 35


Spread Operator with Tuples

You can use the spread operator to extract or merge tuples.

typescript

let tuple1: [number, string] = [1, "hello"];let tuple2: [boolean, ...typeof tuple1] = [true, ...tuple1];console.log(tuple2); // Output: [true, 1, "hello"]


Named Tuple Elements

TypeScript allows naming the elements in tuples to improve readability.

typescript

type Point = [x: number, y: number];let origin: Point = [0, 0];console.log(origin[0]); // Output: 0console.log(origin[1]); // Output: 0


Rest Elements in Tuples

Tuples can have rest elements to represent arrays of a specific type.

typescript

type StringTuple = [string, ...string[]];let words: StringTuple = ["hello", "world", "typescript"];


Use Cases for Tuples

  1. Key-Value Pairs

    typescript

    let keyValue: [string, number] = ["id", 42];

  2. Fixed-Structure Data

    typescript

    let date: [number, number, number] = [2025, 1, 18]; // Year, Month, Day

  3. Return Multiple Values from a Function

    typescript

    function getCoordinates(): [number, number] { return [10, 20];}let [x, y] = getCoordinates();console.log(x, y); // Output: 10 20

  4. Enumerate Data

    typescript

    let user: [id: number, name: string] = [1, "Alice"];


Best Practices

  1. Use tuples when the order and types of elements are fixed and meaningful.
  2. Prefer named tuples for improved code readability.
  3. Avoid using tuples for large or complex data structures; use interfaces or types instead.
  4. Use the readonly modifier for tuples that should not change after initialization.

Conclusion

Tuples in TypeScript provide a powerful way to work with arrays of fixed structure and types. They enable clarity and type safety for scenarios like function returns, key-value pairs, and more. By understanding their features and capabilities, you can leverage tuples effectively in your TypeScript projects.

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql