Tuples in TypeScript
Sometimes we may need to store an array of values of different types. TypeScript provides us with a data type called tuple. A tuple is an array, each element of which has its own hard-coded type. And the array itself has a hard-coded length.
For example, let's make a tuple in which we will store the user's name and age. In the first element of the tuple we will have the name and it will be a string, and in the second element we will have the age and it will be a number. Let's declare the described tuple:
let user: [string, number];
Let's fill our tuple with data when declaring it:
let user: [string, number] = ['john', 31];
Let's print the elements of our tuple:
console.log(user[0]); // 'john'
console.log(user[1]); // 31
Make a tuple that will store the year number and the month number.
Make a tuple that will store the year number and month name.
Make a tuple that will store the year number, month number, and day number.