const user: [string, number, boolean] = ['Alice', 30, true]; console.log(user.length);
The tuple user has exactly three elements: a string, a number, and a boolean. The length property returns the number of elements, which is 3.
const point: [number, number] = [10, 20]; console.log(point[1]);
The tuple point has two numbers. The element at index 1 is the second element, which is 20.
let data: [string, number] = ['hello', 'world'];
The tuple expects a number as the second element, but a string was given. TypeScript reports a type mismatch error.
const rgb: [number, number, number] = [255, 100, 50]; const [red, green, blue] = rgb; console.log(`Red: ${red}, Green: ${green}, Blue: ${blue}`);
The tuple rgb has three numbers representing colors. Destructuring assigns them in order to red, green, and blue. The output shows their values correctly.
result after the operation?const tuple1: [string, number] = ['age', 25]; const tuple2: [string, number] = ['score', 100]; const result = [...tuple1, ...tuple2];
In TypeScript, the spread operator on tuples infers a new tuple type that concatenates the types of the spread tuples in order. Here, spreading two [string, number] tuples results in [string, number, string, number].
