TypeScript — Arrays
Array type syntax
// Number array
let numbers: number[] = [1, 2, 3, 4, 5];
// String array
let names: string[] = ['Alice', 'Bob', 'Charlie'];
// Using generic syntax
let scores: Array<number> = [90, 85, 88];
Readonly arrays
let readonlyArr: readonly number[] = [1, 2, 3];
// readonlyArr.push(4); // Error
Tuple arrays
let tuple: [string, number][] = [['Alice', 25], ['Bob', 30]];
Array methods
let numbers: number[] = [1, 2, 3, 4, 5];
// Map
let doubled = numbers.map(n => n * 2);
// Filter
let evens = numbers.filter(n => n % 2 === 0);
// Reduce
let sum = numbers.reduce((acc, n) => acc + n, 0);
Multi-dimensional arrays
let matrix: number[][] = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
Mini Practice
- Create typed arrays
- Use readonly arrays
- Work with tuple arrays
- Practice array methods
Up Next
Continue with Tuples - Tuple types.
Related Topics
Frequently Asked Questions about Arrays
What is Arrays in TypeScript?
Arrays 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 Arrays?
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 Arrays.
Why is Arrays important in TypeScript?
Arrays is essential for TypeScript development. Understanding this concept will help you write better code and solve real-world problems more effectively.