Open In App

TypeScript Object Tuple Types

TypeScript provides various features to enhance type safety and developer productivity. One of these features includes Object Tuple Types. This feature ensures that an object follows a consistent shape in our code which results in a reduction in runtime errors and improves code quality.

What are Object Tuple Types?

Syntax:

type TupleTypeName = [Type1, Type2, ...];

Where – 



Tuple Array

Example: Here, we declared a tuple type item first and then we created an array of Products of item[] tuple type.

 






type item = [string, number];
  
let Products: item[] = [
    ["Laptop", 999],
    ["Smartphone", 599],
    ["Tablet", 299],
];
console.log(Products);

Output:

[ [ 'Laptop', 999 ], [ 'Smartphone', 599 ], [ 'Tablet', 299 ] ]

Destructure Tuple

Example: Here, we declare a type Triangle create a variable of type Triangle and assign the values to the tuple. To Destructure this tuple and get the values into individual elements, we create variables in this form [a, b, c], so the values will assigned in this manner: let a=10, b=20, c=30.




// Define a triangle tuple type
type Triangle = [number, number, number];
  
// Create a triangle variable of Type Triangle
let triangle: Triangle = [10, 20, 30];
  
// Destructure the tuple into individual variables a, b, c
let [a, b, c] = triangle;
  
// Calculate the perimeter of the Triangle
let perimeter = a + b + c;
  
console.log(`Perimeter of triangle with 
    sides ${a} ,${b}, ${c} : ${perimeter}`);

Output:

Perimeter of triangle with sides 10 ,20, 30 : 60

Optional Tuple

Example: In this example we have made the number property optional, so while creating a variable of mentor type. the age property may be present or may not be present.




// Define an optional tuple type for a Mentor
type Mentor = [string, number?];
  
// Create a mentor with an optional age
let Mony: Mentor = ["Mony"];
let Lala: Mentor = ["Lala", 21];
  
console.log(Mony); // [ 'Mony' ]
console.log(Lala);   // [ 'Lala', 21 ]

Output:

[ 'Mony' ]
[ 'Lala', 21 ]

Tuple with Rest Elements

Example: In this example, we created a rest element of type number, a dynamic list, which can hold any number of values passed in the tuple but only of number type. we are trying to track gaming scores.




type GameScores = [string, number, ...number[]];
  
let CricketScores: GameScores = ["Dhoni", 50, 75, 90, 65, 80];
  
console.log(CricketScores);

Output:

[ 'Dhoni', 50, 75, 90, 65, 80 ]

Tuple with Rest Parameters and Arguments

We can use tuple types with rest parameters and rest arguments to handle functions that take a variable number of arguments while preserving type safety.

Example: Let’s consider this example, here we have a tuple type with properties string, number, …string[] (can hold any number of string values, it is a dynamic list). Function call happens with the… spread operator which separates each property into individual variables and in the function parameter, this is handled by the rest parameter.




function printMembersNameandSkills(...details: [string, number, 
...string[]]) {
    for (let i = 0; i < details.length; i++) {
        if (i == 0) console.log(`Name : ${details[i]}`);
        if (i == 1) console.log(`E-ID : ${details[i]}`);
        else console.log(`Skills : ${details[i]}`);
    }
}
type member = [string, number, ...string[]];
  
let gfgTeamMembers: member = ["Roland", 1234, 
    "Java", "C#", "Python", "TypeScript"];
  
printMembersNameandSkills(...gfgTeamMembers);

Output:

Name : Roland
Skills : Roland
E-ID : 1234
Skills : Java
Skills : C#
Skills : Python
Skills : TypeScript

Article Tags :