What is Tuple in TypeScript ?
A tuple is a type in TypeScript that is used to represent an array in which the type of a fixed number of elements is known, but not for all the elements. It provides a way to represent the ordered set of the element types for certain elements in a TypeScript array. A tuple always has a fixed number of elements and each one of them has their types associated with them.
Syntax:
const tupleName: [ type1, type2, type3 ];
Example: The below code will explain the use of the tuple in TypeScript with its practical implementation.
Javascript
const myTuple: [number, string, boolean] = [2009, "GFG" , true ]; console.log( "Boolean value: " , myTuple[2], "," , "String Value: " , myTuple[1], "," , "Number Value: " , myTuple[0]); |
Output:
Boolean value: true, String Value: GFG, Number Value: 2009