TypeScript Array

TypeScript is a superset of JavaScript that provides optional static typing, making it easier to write complex code. One of the features that TypeScript provides is the ability to define arrays. An array is a collection of elements of the same type. In this lesson, we will go over what TypeScript arrays are, how they work, and how to use them with code examples.

What is an Array in TypeScript?

In TypeScript, an array is a collection of elements of the same type. Arrays are similar to lists in other programming languages. You can create an array using the following syntax:

let numbers: number[] = [1, 2, 3, 4];

In this example, we create an array called numbers that contains four elements of type number.

You can also create an array using the Array keyword:

let strings: Array<string> = ['hello', 'world'];

In this example, we create an array called strings that contains two elements of type string.

How to Use an Array in TypeScript

Once you have defined an array, you can use it in your code like this:

let numbers: number[] = [1, 2, 3, 4];
console.log(numbers[0]); // Output: 1
console.log(numbers.length); // Output: 4

In this example, we declare a variable called numbers that contains an array of four elements. We then use the array index to access the first element and print it to the console. We also use the length property of the array to print the number of elements in the array.

You can also use array methods like push, pop, shift, and unshift to add and remove elements from an array:

let numbers: number[] = [1, 2, 3, 4];
numbers.push(5); // Add element to end of array
numbers.pop(); // Remove element from end of array
numbers.unshift(0); // Add element to beginning of array
numbers.shift(); // Remove element from beginning of array

In this example, we use the push method to add an element to the end of the numbers array, the pop method to remove an element from the end of the array, the unshift method to add an element to the beginning of the array, and the shift method to remove an element from the beginning of the array.

You can also use array methods like map, filter, and reduce to manipulate the elements of an array:

let numbers: number[] = [1, 2, 3, 4];
let doubledNumbers = numbers.map(num => num * 2);
let filteredNumbers = numbers.filter(num => num > 2);
let sumOfNumbers = numbers.reduce((prev, curr) => prev + curr);

In this example, we use the map method to create a new array called doubledNumbers that contains the elements of the numbers array multiplied by two. We use the filter method to create a new array called filteredNumbers that contains only the elements of the numbers array that are greater than two. We use the reduce method to calculate the sum of the elements in the numbers array.

Conclusion

In conclusion, arrays are a powerful feature of TypeScript that make it easier to work with collections of elements of the same type. They help make your code more readable, maintainable, and less error-prone. With arrays, you can define a collection of elements of the same type and use them in your.