Find the Length Tuple in TypeScript ?
Last Updated :
29 May, 2024
In TypeScript, a Tuple is the data structure that allows storing a fixed number of elements of different types in a specific order. We can find the length of a tuple using various approaches that are implemented in this article in terms of examples.
There are several approaches available in TypeScript for finding the length of a tuple which are as follows:
Using the length property
In this approach, we are using the length
property directly on the tuple variable to obtain its length, which counts the number of elements in the tuple.
Syntax:
tuple.length
Example: The below example uses the length property to find the length of a dictionary in TypeScript.
JavaScript
let tuple: [number, string, boolean] = [2009, "GFG", true];
let tupleLength = tuple.length;
console.log("Tuple Length:", tupleLength);
Output:
"Tuple Length:", 3
Using a for...in loop
In this approach, we are using a for...in loop to iterate through the tuple and increment a counter variable (tupleLength) for each element encountered, effectively counting the number of elements in the tuple.
Syntax:
for (variable in object) {
// code
Example: The below example uses the for…in loop to find the length of a tuple in TypeScript.
JavaScript
let tuple: [number, string, boolean] = [2009, "GFG", true];
let tupleLength = 0;
for (let _ in tuple) {
tupleLength++;
}
console.log("Tuple Length:", tupleLength);
Output:
"Tuple Length:", 3
Using the reduce method
In this approach, we are using the reduce method to accumulate the length of the tuple by adding 1 to the accumulator (acc) for each element encountered during the reduction process.
Syntax:
array.reduce(callback: (accumulator: T, currentValue: T, currentIndex:
number, array: T[]) => T, initialValue?: T): T;
Example: The below example uses the reduce function to find the length of a tuple in TypeScript.
JavaScript
let tuple: [number, string, boolean] = [2009, "GFG", true];
let tupleLength = tuple
.reduce((acc: number, curr: any) => acc + 1, 0);
console.log("Tuple Length:", tupleLength);
Output:
"Tuple Length:", 3
Using TypeScript's Type Utilities
In this approach, we will use TypeScript's type utilities to determine the length of a tuple. This method leverages TypeScript's type system to compute the length at compile time, ensuring type safety and potentially improving performance by avoiding runtime calculations.
Syntax:
type Length<T extends readonly any[]> = T['length'];
Example: In this example we defines a Length type utility to extract the length of a tuple. It infers the length of the tuple tuple and prints it using tuple.length
JavaScript
type Length<T extends readonly any[]> = T['length'];
let tuple: [number, string, boolean] = [2009, "GFG", true];
type TupleLength = Length<typeof tuple>;
console.log("Tuple Length:", tuple.length);
Output:
Tuple Length: 3
Using recursion:
In this approach, we define a recursive function that iterates through the tuple and counts the elements until reaching the end of the tuple. Recursion allows us to traverse through each element of the tuple without needing to know its length beforehand.
Example:
JavaScript
function tupleLength<T extends unknown[]>(tuple: T): number {
function countElements(tuple: unknown[], index: number): number {
if (tuple[index] !== undefined) {
return 1 + countElements(tuple, index + 1);
} else {
return 0;
}
}
return countElements(tuple, 0);
}
const tuple: [string, number, boolean] = ['apple', 5, true];
const length = tupleLength(tuple);
console.log("Tuple Length:", length);
Output
Tuple Length: 3
Similar Reads
Find the Length of a Dictionary in TypeScript In TypeScript, to find the length of a dictionary, there is no built-in property for objects that determines the length of the dictionary. The length of the input dictionary can be retrieved by counting the pairs of key values in it. There are several approaches available in TypeScript for finding t
4 min read
Explain the Tuple Types in TypeScript TypeScript is an open-source object-oriented programming language developed and maintained by Microsoft Corporation. It is a strongly typed language and was first introduced in 2012. TypeScript is a strict superset of JavaScript, which means that anything implemented in JavaScript can be implemented
3 min read
How to map Enum/Tuple to Object in TypeScript ? Mapping enum or tuple values to objects is a common practice in TypeScript for handling different data representations. This article explores various methods to map enumerations (enums) and tuples to objects, providing examples to illustrate each method.Table of ContentManually mapping Enum to Objec
3 min read
TypeScript Defining a Union Type In this article, we are going to learn about Defining a Union Type in Typescript. TypeScript is a popular programming language used for building scalable and robust applications. In TypeScript, a union type allows a variable to have one of several possible types. You can define a union type by using
3 min read
TypeScript Conditional Types In TypeScript, conditional types enable developers to create types that depend on a condition, allowing for more dynamic and flexible type definitions.They follow the syntax T extends U ? X : Y, meaning if type T is assignable to type U, the type resolves to X; otherwise, it resolves to Y.Conditiona
4 min read