Tuples have had a few enhancements in TypeScript 3, so that they can be used with the popular rest and spread JavaScript syntax. Before we get into the specific enhancements, we'll go through what tuples are, along with what the rest and spread syntax is. A tuple is like an array but the number of elements are fixed. It's a simple way to structure data and use some type safety.
Let's have a play with tuples:
- In the TypeScript playground, let's enter the following example of a tuple variable:
let product: [string, number];
We've initialized a product variable to a tuple type with two elements. The first element is a string and the second a number.
- We can store a product name and its unit price in the product variable on the next line, as follows:
product = ["Table", 500];
- Let's try to store the product name and unit price the...