TypeScript — Tuples
Basic tuple
let person: [string, number] = ['Alice', 25];
Accessing elements
let person: [string, number] = ['Alice', 25];
console.log(person[0]); // string
console.log(person[1]); // number
Optional elements
let record: [string, number, boolean?] = ['Alice', 25];
Rest elements
let rest: [string, ...number[]] = ['Alice', 1, 2, 3];
Readonly tuples
let readonlyTuple: readonly [string, number] = ['Alice', 25];
Named tuples
type Person = [name: string, age: number];
let person: Person = ['Alice', 25];
Mini Practice
- Create tuples
- Access elements
- Use optional elements
- Create readonly tuples
Up Next
Continue with Enums - Enumeration types.
Related Topics
Frequently Asked Questions about Tuples
What is Tuples in TypeScript?
Tuples is a fundamental concept in TypeScript. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Tuples?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Tuples.
Why is Tuples important in TypeScript?
Tuples is essential for TypeScript development. Understanding this concept will help you write better code and solve real-world problems more effectively.