Mapagam
  • JavaScript 
  • Web APIs 
  • TypeScript 
  • React 

How to Work with Arrays in TypeScript

Posted on April 16, 2025 • 6 min read • 1,144 words
Share via
Mapagam
Link copied to clipboard

Learn how to work with arrays in TypeScript, including types, methods, and best practices for safer, more efficient coding.s

On this page
1. Introduction to Arrays in TypeScript 1.1 Array Declaration in TypeScript 2. Array Types in TypeScript 2.1 Homogeneous Arrays 2.2 Heterogeneous Arrays 2.3 Multi-dimensional Arrays 3. Common Array Methods in TypeScript 3.1 Array.push() 3.2 Array.pop() 3.3 Array.shift() 3.4 Array.unshift() 3.5 Array.concat() 3.6 Array.slice() 4. Advanced Array Techniques in TypeScript 4.1 Array.map() 4.2 Array.filter() 4.3 Array.reduce() 4.4 Array.find() 4.5 Array.sort() 5. Type Checking and Type Inference for Arrays in TypeScript 5.1 Type Inference 5.2 Explicit Type Declaration 5.3 Array of Unknown Types 5.4 Readonly Arrays 6. Conclusion

Arrays are one of the most fundamental data structures in programming. They are used to store multiple values in a single variable, and in TypeScript, they are no different. TypeScript, being a statically typed superset of JavaScript, provides a strong typing system that allows for safer, more predictable code when working with arrays.

1. Introduction to Arrays in TypeScript

Arrays are ordered collections of elements, typically of the same type, that can store multiple values in a single variable. In TypeScript, arrays are objects, just like in JavaScript, but with additional type safety features that help developers prevent common programming errors.

1.1 Array Declaration in TypeScript

In TypeScript, you can define arrays in two primary ways: using the array literal syntax or the generic Array<T> type.

1.1.1 Using Array Literal Syntax

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

This syntax specifies that the array numbers can only contain elements of type number.

1.1.2 Using Array Generic Type

let names: Array<string> = ["Alice", "Bob", "Charlie"];

Here, we specify that the array names should only contain elements of type string. This syntax is functionally equivalent to the array literal syntax but is useful when working with more complex generics.

2. Array Types in TypeScript

TypeScript arrays come with various ways of specifying the type of elements they store. Understanding array types is key to writing robust TypeScript code.

2.1 Homogeneous Arrays

A homogeneous array is an array where all elements are of the same type.

let numbers: number[] = [10, 20, 30]; // Valid
let mixed: number[] = [1, "two", 3];  // Error: "two" is not a number

In this example, the numbers array is a valid homogeneous array, whereas the mixed array is invalid because it contains a string element.

2.2 Heterogeneous Arrays

In some cases, you may need an array that can store elements of different types. TypeScript allows you to define such arrays using tuples.

2.2.1 Using Tuples

Tuples are arrays with a fixed number of elements, where each element can have a different type.

let userInfo: [string, number] = ["Alice", 25];

In this case, the array userInfo contains exactly two elements: the first being a string and the second being a number.

2.3 Multi-dimensional Arrays

A multi-dimensional array is an array of arrays. These are useful when you need to represent grids or matrices.

let matrix: number[][] = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
];

This example represents a 3x3 matrix where each element is a number.

3. Common Array Methods in TypeScript

TypeScript arrays are equipped with a wide range of methods to manipulate and work with array elements. Below are some commonly used array methods in TypeScript:

3.1 Array.push()

The push() method adds one or more elements to the end of an array.

let numbers: number[] = [1, 2, 3];
numbers.push(4);  // numbers becomes [1, 2, 3, 4]

3.2 Array.pop()

The pop() method removes the last element from an array and returns that element.

let numbers: number[] = [1, 2, 3];
let last = numbers.pop();  // last = 3, numbers becomes [1, 2]

3.3 Array.shift()

The shift() method removes the first element from an array and returns that element.

let numbers: number[] = [1, 2, 3];
let first = numbers.shift();  // first = 1, numbers becomes [2, 3]

3.4 Array.unshift()

The unshift() method adds one or more elements to the beginning of an array.

let numbers: number[] = [1, 2, 3];
numbers.unshift(0);  // numbers becomes [0, 1, 2, 3]

3.5 Array.concat()

The concat() method is used to merge two or more arrays.

let array1: number[] = [1, 2];
let array2: number[] = [3, 4];
let combined = array1.concat(array2);  // combined = [1, 2, 3, 4]

3.6 Array.slice()

The slice() method returns a shallow copy of a portion of an array.

let numbers: number[] = [1, 2, 3, 4];
let sliced = numbers.slice(1, 3);  // sliced = [2, 3]

4. Advanced Array Techniques in TypeScript

Working with arrays can get more complex when dealing with larger datasets or when specific data manipulation is required. Let’s look at some advanced techniques for working with arrays in TypeScript.

4.1 Array.map()

The map() method allows you to transform every element in the array into a new form.

let numbers: number[] = [1, 2, 3, 4];
let squared = numbers.map(num => num * num);  // squared = [1, 4, 9, 16]

4.2 Array.filter()

The filter() method allows you to create a new array that only contains elements that meet a specific condition.

let numbers: number[] = [1, 2, 3, 4, 5];
let evenNumbers = numbers.filter(num => num % 2 === 0);  // evenNumbers = [2, 4]

4.3 Array.reduce()

The reduce() method applies a function to each element in the array (from left to right) to reduce it to a single value.

let numbers: number[] = [1, 2, 3, 4];
let sum = numbers.reduce((acc, num) => acc + num, 0);  // sum = 10

4.4 Array.find()

The find() method returns the first element in the array that satisfies a given condition.

let numbers: number[] = [1, 2, 3, 4];
let found = numbers.find(num => num > 2);  // found = 3

4.5 Array.sort()

The sort() method is used to sort the elements of an array in place.

let numbers: number[] = [4, 1, 3, 2];
numbers.sort();  // numbers = [1, 2, 3, 4]

5. Type Checking and Type Inference for Arrays in TypeScript

TypeScript’s type system ensures that arrays are used correctly and helps prevent type-related errors. Here’s how TypeScript helps with type checking and inference when working with arrays.

5.1 Type Inference

TypeScript automatically infers the type of an array based on its initial elements.

let numbers = [1, 2, 3];  // TypeScript infers this as number[]

5.2 Explicit Type Declaration

You can explicitly declare the type of an array, even if the array is empty, by specifying the type inside square brackets.

let numbers: number[] = [];

5.3 Array of Unknown Types

Sometimes you may want to create an array that can hold elements of any type. TypeScript allows this by using the any[] type.

let mixedArray: any[] = [1, "two", true, null];

5.4 Readonly Arrays

TypeScript also offers a ReadonlyArray<T> type for arrays that should not be modified after creation.

let numbers: ReadonlyArray<number> = [1, 2, 3];
// numbers.push(4);  // Error: Property 'push' does not exist on type 'ReadonlyArray<number>'.

6. Conclusion

Arrays in TypeScript provide developers with a powerful way to manage collections of data. With TypeScript’s static typing, array operations are safer and more predictable. Whether you are working with simple arrays or more advanced data structures like tuples and multi-dimensional arrays, TypeScript’s array functionality gives you the tools you need to write clean, error-free code.

By mastering array methods like push(), map(), filter(), and reduce(), as well as understanding type safety features like type inference and ReadonlyArray, you can build robust applications in TypeScript that handle complex data more efficiently.

TypeScript Arrays   TypeScript Array Methods   Working With Arrays   TypeScript Tips   TypeScript Best Practices  
TypeScript Arrays   TypeScript Array Methods   Working With Arrays   TypeScript Tips   TypeScript Best Practices  
 TypeScript Tuples: What They Are and How to Use Them
How to Extend Interfaces in TypeScript 

More Reading!

  1. Beginner’s Guide to JavaScript Functions (With Best Practices)
  2. Understanding TypeScript Decorators: A Step-by-Step Guide
  3. TypeScript Tuples: What They Are and How to Use Them
  4. How to Use TypeScript Objects Like a Pro
  5. TypeScript Variables: Let, Const, and Var Explained
On this page:
1. Introduction to Arrays in TypeScript 1.1 Array Declaration in TypeScript 2. Array Types in TypeScript 2.1 Homogeneous Arrays 2.2 Heterogeneous Arrays 2.3 Multi-dimensional Arrays 3. Common Array Methods in TypeScript 3.1 Array.push() 3.2 Array.pop() 3.3 Array.shift() 3.4 Array.unshift() 3.5 Array.concat() 3.6 Array.slice() 4. Advanced Array Techniques in TypeScript 4.1 Array.map() 4.2 Array.filter() 4.3 Array.reduce() 4.4 Array.find() 4.5 Array.sort() 5. Type Checking and Type Inference for Arrays in TypeScript 5.1 Type Inference 5.2 Explicit Type Declaration 5.3 Array of Unknown Types 5.4 Readonly Arrays 6. Conclusion
Follow me

I work on everything coding and technology

   
Mapagam
Mapagam is your go-to resource for all things related to frontend development. From the latest frameworks and libraries to tips, tutorials, and best practices, we dive deep into the ever-evolving world of web technologies.
Licensed under Creative Commons (CC BY-NC-SA 4.0).
 
Frontend
JavaScript 
Web Api 
TypeScript 
React 
Social
Linkedin 
Github 
Mapagam
Code copied to clipboard