Programmingempire
In this post on the Significance of Tuples in TypeScript, I will explain to you the benefits of tuples data structure in TypeScript with the help of a few examples.
As a matter of fact, when we need to store lots of values together, then we use an array. However, all elements of the array must have the same type. In contrast, tuples can hold values of varying data types and this is one of the most significant benefits of using tuples.
What is a Tuple?
Basically, a tuple is a data structure in TypeScript that can store multiple values with different data types.
Significance of Tuples in TypeScript
We can list the significance of tuples in TypeScript as given below.
- Tuples can store multiple values.
- The elements of the tuples need not have the same data type.
- In fact, a tuple may contain another tuple also.
- We can have functions that return more than one value by using tuple as the return type.
- When you don’t want to create a class just for storing several elements, you can use a tuple instead.
- Also, you can pass a tuple as a parameter to a function.
- Finally, the use of tuple makes your code more concise and you can develop the desired functionality quickly. As a result, the application development becomes more productive.
As an illustration, let us consider following examples of using tuples.
Example: Creating a tuple and accessing its individual elements.
In this example, we create a tuple with two elements of data type string and number respectively. Further, we retrieve the individual elements of the tuple and perform some operations on them.
let x: [string, number]
x=["Jolly", 1]
var a=x[1]+10
var v=x[0].substring(0,1)
var v1=v+""+a
console.log(x[0]+" "+x[1]+" "+v1)
Output
Example: Returning a tuple from a function
In the example given below, the function compute() performs some arithmetic computation on the two numbers passed as parameters and need to return four values. Therefore, it returns all the four values as a collection using a tuple.
function compute(a: number, b:number): [x:number, y:number, z:number, w:number]
{
var p=a+b
var q=a-b
var r=a*b
var t=a/b
return [p, q, r, t]
}
var a=compute(25, 32)
console.log(a[0]+" "+a[1]+" "+a[2]+" "+a[3])
Output
Example: A Tuple Returning the Related Values
The following example shows the use of a tuple when a function needs to return certain values together. In this example, the tuple holds the values which are related to each other. As shown, the function Select() takes the array of objects of the Book class and returns a string and a particular object of the Book class as a tuple.
class Book
{
title: string
author: string
price: number
constructor(a:string, b:string, c:number)
{
this.title=a
this.author=b
this.price=c
}
show(){
console.log("Book Title: "+this.title+" Author: "+this.author+" Price: "+this.price)
}
}
//Creating Book Object
console.log("Display single object: ")
var b:Book
b=new Book("C#", "A", 300)
b.show()
//Creating Array of book objects
let bookarray=[new Book("PHP", "Alexaner Clyde", 438), new Book("C#", "B", 500), new Book("Java", "C", 345), new Book("python", "D", 670)]
//Display all array elements using foreach
console.log("nDisplay book array: ")
bookarray.forEach(function(v){ v.show()});
//passing bookarray as parameter to a function
function Select(b:Book[]): [s:string, t:Book]
{
let name="Jim"
var b1:Book
b1=b[2]
return [name, b1]
}
var t=Select(bookarray)
console.log("nDisplay Function Return Values: ")
console.log(t[0]+" selected the book: ")
t[1].show()
Output
Summary
In this article on the Significance of Tuples in TypeScript, I have explained what is a tuple and also enumerated several benefits of using a tuple. Further, I have demonstrated how to create a tuple in TypeScript and retrieve its individual elements. Moreover, it is also demonstrated that when you want to return multiple values from a function, then you can also use a tuple. Similarly, when you want to return some related values together, then also you can make use of a tuple and this is demonstrated in the last example.