TypeScript

9.Understanding Tuples in TypeScript

Understanding Tuples in TypeScript

In TypeScript, arrays provide a way to store multiple values of the same data type. But what if you need to store multiple values of different types in a single variable? This is where Tuples come into play. Tuples in TypeScript allow you to store a collection of values that can be of different types, making them ideal for cases where you want to represent multiple pieces of information in a single variable.

In this blog post, we’ll explore Tuples in TypeScript, including their syntax, how to declare them, and the various operations you can perform on them.


What is a Tuple?

A Tuple is an ordered collection of elements, where each element can be of a different data type. Unlike arrays, which only support values of the same data type, tuples allow you to store multiple values of different types in a single collection. For example, you might want to store an ID number, a name, and a price in one variable.

Syntax for Declaring Tuples

The syntax for declaring a tuple is simple. You use square brackets [] to enclose the elements of the tuple. The elements inside the tuple can be of different data types.

Example:

var basket = [200, "Biscuit", 23.50, "Jeans"];

In the above example:

  • 200 is a number (item 1).

  • "Biscuit" is a string (item 2).

  • 23.50 is a number (item 3).

  • "Jeans" is a string (item 4).

These elements are stored together in a single tuple called basket.


Displaying Tuple Values

Since a tuple is essentially an ordered collection like an array, you can access its elements using their index. Just like arrays, tuple indices start from 0.

Example:

var basket = [200, "Biscuit", 23.50, "Jeans"];
console.log(basket[0]); // Output: 200
console.log(basket[1]); // Output: Biscuit
console.log(basket[2]); // Output: 23.50

In the above example, we access the elements of the basket tuple using their indices. The first item is at index 0, the second item at index 1, and so on.


Declaring an Empty Tuple

You can also declare an empty tuple and then populate it later. This allows you to create a tuple without initially specifying any values.

Example:

var bags: [string, number, string] = []; // Empty Tuple

// Adding elements
bags[0] = 'Pencil';
bags[1] = 500;
bags[2] = 'Tiffin Box';

console.log(bags[0]); // Output: Pencil
console.log(bags[1]); // Output: 500
console.log(bags[2]); // Output: Tiffin Box

In the above example:

  • We declare an empty tuple bags and later assign values to each index.

  • The tuple elements are defined to be of types [string, number, string], which means the first item is a string, the second is a number, and the third is a string.


Tuple Functions

Tuples in TypeScript support various functions, just like arrays. You can push new items into a tuple, pop items from it, and more.

Example: Using push() and pop() on Tuples

var courses = ['Java', 'C++', 'C', 'TypeScript'];

console.log("Courses before push: " + courses.length); // Returns tuple size

// Removing the last item
console.log("Items before pop: " + courses.length);
console.log(courses.pop() + " popped from the tuple");
console.log("Courses after pop: " + courses.length);

// Adding a new item
courses.push('PHP');
console.log("Courses after push: " + courses.length);
console.log("Courses after push: " + courses);

Output:

Courses before push: 4
Items before pop: 4
TypeScript popped from the tuple
Courses after pop: 3
Courses after push: 4
Courses after push: ['Java', 'C++', 'C', 'PHP']
  • pop(): Removes the last item from the tuple.

  • push(): Adds a new item to the end of the tuple.


Editing Tuples

You can update the values in a tuple just like you can with an array. To change a value, simply access the item by its index and assign a new value to it.

Example:

var courses = ['Java', 'C++', 'C', 'TypeScript'];

console.log("Tuple value at index 0: " + courses[0]); // Output: Java

// Update the first item in the tuple
courses[0] = 'ASP.NET';

console.log("Tuple value at index 0 changed to: " + courses[0]); // Output: ASP.NET

In this example, we update the first element of the courses tuple from 'Java' to 'ASP.NET'.


Destructuring a Tuple

Tuple destructuring works similarly to array destructuring. You can break a tuple into individual values and assign them to separate variables.

Example:

var nums: [number, number] = [100, 200];
var [x, y] = nums;

console.log(x); // Output: 100
console.log(y); // Output: 200

In this example:

  • We destructure the nums tuple and assign the values to variables x and y.

  • The first item in the tuple (100) is assigned to x, and the second item (200) is assigned to y.


Conclusion

Tuples in TypeScript offer a flexible way to store multiple values of different types in a single variable. They are particularly useful when you want to store multiple pieces of related information in one collection. In this blog post, we covered the following key concepts:

  • Declaring Tuples: Using arrays to store heterogeneous data types.

  • Accessing Tuple Elements: Using indices to access values in a tuple.

  • Manipulating Tuples: Using methods like push(), pop(), and destructuring to work with tuples.

  • Editing Tuples: Changing values in a tuple after initialization.

Tuples provide a great way to organize and manage heterogeneous data in your TypeScript projects. They are especially useful when you need to work with collections of values that have different data types, such as pairs of values or records with multiple fields of different types.

Leave a Reply

Your email address will not be published. Required fields are marked *