Explain the Tuple Types in TypeScript
Last Updated :
22 Jul, 2024
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 using TypeScript, along with the choice of adding enhanced features. Essentially, every existing JavaScript code is valid TypeScript code.
In this article, we will learn about the tuple data type in TypeScript.
Tuple in TypeScript
A tuple is a new data type introduced by TypeScript. Unlike arrays, tuples can hold values of different data types. This flexibility allows tuples to be more versatile in certain scenarios compared to traditional arrays.
var geek: [boolean, string] = [true, "Aayush"];
Creating Tuples
Tuples can be created with elements of various data types. For example, consider the following tuple with element types boolean and string:
var nice: [boolean, string, number] = [true, "Aayush", 1];
You can also create tuples with multiple data types:
var nice: [boolean, string, number] = [true, "Aayush", 1];
Accessing Tuple Elements
As a tuple is a form of an array, we can use indexing to access individual elements in a tuple, just the way I have done in the following code:
Example:
JavaScript
var geek: [boolean, string] = [true, "Aayush"];
console.log(geek[0]);
console.log(geek[1]);
Output:
true
"Aayush"
Add Elements into Tuple
The push() function allows you to add additional members to a tuple. Let us understand this by looking an example, in the following code we are pushing a string to the already created geek tuple.
Example:
JavaScript
var geek: [boolean, string] = [true, "Aayush"];
geek.push("hello");
console.log(geek)
Output:
[true, "Aayush", "hello"]
You might also be tempted to push something other than string or boolean into the tuple geek, however, this will lead to an error. For example, running the following code
var geek: [boolean, string] = [true, "Aayush"];
geek.push(20);
console.log(geek)
gives the following error:
Argument of type '20' is not assignable to parameter of type 'string | boolean'.
Remove Elements from Tuple
The pop() function can be used to remove the last element from the tuple:
Example:
JavaScript
var geek: [boolean, string] = [true, "Aayush"];
geek.pop();
console.log(geek)
Output:
[true]
Note: As tuples are a form of an array, you can easily use array functions like concat, map, etc., on a tuple.
Similar Reads
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 Obj
5 min read
Find the Length Tuple in TypeScript ? 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 TypeScri
3 min read
What are template literal types in Typescript ? Template literal types in TypeScript allow the construction of new string literal types by combining existing string literal types using template literal syntax.They enable the creation of complex string patterns by embedding unions and other literal types within template literals.This feature enhan
3 min read
Explain Type assertions in TypeScript In TypeScript, type assertions allow developers to override the compiler's inferred type, informing it of the specific type of a value. Type assertions are purely compile-time constructs and do not alter the runtime behavior of the code. They are particularly useful when interfacing with APIs or thi
3 min read
Data types in TypeScript In TypeScript, a data type defines the kind of values a variable can hold, ensuring type safety and enhancing code clarity.Primitive Types: Basic types like number, string, boolean, null, undefined, and symbol.Object Types: Complex structures including arrays, classes, interfaces, and functions.Prim
3 min read
TypeScript Object readonly Tuple Types In this article, we will learn about Object readonly Tuple Types in Typescript. In TypeScript, an object with read-only tuple types is a type that represents an object with properties where each property has a specific value and the order of properties is fixed, just like a tuple. However, unlike re
3 min read
How to Use Variadic Tuple Types in Typescript? Types of Variadic Tuple are essential TypeScript elements that furnish an opportunity for constructing a variable number of elements tuples, in particular, this feature is important when you need to create such functions or structures which interact with variable lengthed and typed tuples.Variadic t
3 min read
TypeScript Generic Object Types TypeScript Generic Object Types allow you to create flexible and reusable type definitions for objects. These generic types can work with different shapes of objects while providing type safety, ensuring your code is both robust and adaptable. They are particularly useful for creating functions or c
3 min read
What is the Record Type in TypeScript ? In TypeScript, the Record type is a utility type that represents an object type with keys and values of a specific type. It is often used when you want to define a type for the keys and values of an object. In this article, we will learn what is the Record type in TypeScript. Syntax:Record<Keys,
2 min read
Higher-Order Types in TypeScript Higher-order types are among the advanced aspects of Typescript that give priority to types as first-class citizens, similar to higher-order functions of JavaScript that accept a function as an argument or return a function, higher-order types can accept types or return types.These are the following
6 min read