Lesson 15 of 60 – Array Type
25%

Array Type in TypeScript

An array is used to store multiple values in a single variable. TypeScript allows you to specify the type of values that an array can contain, such as strings, numbers, or booleans.

Note: TypeScript arrays are strongly typed. For example, number[] means the array should contain numbers, while string[] means it should contain strings.

1. What is an Array?

An array is a collection of multiple values stored under one variable.

let numbers: number[] = [
    10,
    20,
    30
];

Here, numbers contains three numeric values.

2. Number Array

The number[] syntax is used for an array containing numbers.

let marks: number[] = [
    80,
    75,
    90,
    85
];

console.log(marks);

Every element in this array must be a number.

3. String Array

The string[] syntax is used for an array containing strings.

let students: string[] = [
    "Amit",
    "Rahul",
    "Priya"
];

console.log(students);

4. Boolean Array

You can create an array containing Boolean values using boolean[].

let results: boolean[] = [
    true,
    false,
    true
];

console.log(results);

5. Array Type Annotation

You can explicitly specify the type of an array.

let ages: number[] = [
    18,
    20,
    22
];

The : number[] annotation tells TypeScript that the array contains numbers.

6. Array Type Inference

TypeScript can automatically infer the type of an array from its initial values.

let numbers = [
    10,
    20,
    30
];

TypeScript infers this as a number array.

7. Creating an Empty Typed Array

When creating an empty array, you can specify its type explicitly.

let marks: number[] = [];

The array can now receive number values.

8. Adding Items with push()

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

let numbers: number[] = [
    10,
    20
];

numbers.push(30);

console.log(numbers);

Output:

[10, 20, 30]

9. Adding Multiple Items

You can add multiple values using push().

let students: string[] = [
    "Amit"
];

students.push(
    "Rahul",
    "Priya"
);

console.log(students);

10. Removing the Last Item

The pop() method removes the last item from an array.

let numbers: number[] = [
    10,
    20,
    30
];

numbers.pop();

console.log(numbers);

Output:

[10, 20]

11. Array Length

The length property returns the number of elements in an array.

let students: string[] = [
    "Amit",
    "Rahul",
    "Priya"
];

console.log(students.length);

Output:

3

12. Accessing Array Elements

Array elements are accessed using their index. The first element has index 0.

let students: string[] = [
    "Amit",
    "Rahul",
    "Priya"
];

console.log(students[0]);
console.log(students[1]);

Output:

Amit
Rahul

13. Changing an Array Element

You can change an element by using its index.

let marks: number[] = [
    70,
    80,
    90
];

marks[0] = 85;

console.log(marks);

Output:

[85, 80, 90]

14. Looping Through an Array

A for loop can be used to access every array element.

let numbers: number[] = [
    10,
    20,
    30
];

for (let i = 0; i < numbers.length; i++) {

    console.log(numbers[i]);

}

15. for...of Loop

The for...of loop provides a simple way to access array values.

let students: string[] = [
    "Amit",
    "Rahul",
    "Priya"
];

for (const student of students) {

    console.log(student);

}

16. includes() Method

The includes() method checks whether an array contains a particular value.

let numbers: number[] = [
    10,
    20,
    30
];

console.log(
    numbers.includes(20)
);

Output:

true

17. indexOf() Method

The indexOf() method returns the index of a value.

let students: string[] = [
    "Amit",
    "Rahul",
    "Priya"
];

console.log(
    students.indexOf("Rahul")
);

Output:

1

18. slice() Method

The slice() method creates a new array containing a selected portion of an array.

let numbers: number[] = [
    10,
    20,
    30,
    40
];

let result: number[] =
    numbers.slice(1, 3);

console.log(result);

Output:

[20, 30]

19. join() Method

The join() method combines array elements into a string.

let students: string[] = [
    "Amit",
    "Rahul",
    "Priya"
];

let result: string =
    students.join(", ");

console.log(result);

Output:

Amit, Rahul, Priya

20. map() Method

The map() method creates a new array by applying a function to every element.

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

let doubled: number[] =
    numbers.map(
        (number: number) => number * 2
    );

console.log(doubled);

Output:

[2, 4, 6]

21. filter() Method

The filter() method creates a new array containing elements that satisfy a condition.

let marks: number[] = [
    30,
    60,
    80,
    35
];

let passed: number[] =
    marks.filter(
        (mark: number) => mark >= 40
    );

console.log(passed);

Output:

[60, 80]

22. Array of Objects

An array can contain objects with a defined structure.

let students: {
    name: string;
    age: number;
}[] = [

    {
        name: "Amit",
        age: 20
    },

    {
        name: "Rahul",
        age: 21
    }

];

This is useful when working with structured data.

23. Readonly Arrays

TypeScript provides readonly arrays when you do not want the array itself to be modified through that reference.

let numbers: readonly number[] = [
    10,
    20,
    30
];

Methods that modify the array, such as push(), cannot be used on a readonly array.

24. Arrays in Functions

Functions can receive arrays as parameters and return arrays.

function doubleNumbers(
    numbers: number[]
): number[] {

    return numbers.map(
        (number: number) => number * 2
    );

}

console.log(
    doubleNumbers([1, 2, 3])
);

25. Arrays in Objects

Arrays can be used as properties inside objects.

let student: {
    name: string;
    marks: number[];
} = {

    name: "Rahul",

    marks: [
        80,
        75,
        90
    ]

};

console.log(student.marks);

26. Arrays in React

Arrays are frequently used in React applications to store lists of items.

const students: string[] = [
    "Amit",
    "Rahul",
    "Priya"
];

function App() {

    return (
        <div>

            {students.map(
                (student: string) =>
                    <p key={student}>
                        {student}
                    </p>
            )}

        </div>
    );

}

27. Arrays in React Native

Arrays are very important in React Native applications. They can store users, products, students, messages, posts, and API data.

const students: string[] = [
    "Amit",
    "Rahul",
    "Priya"
];

students.map(
    (student: string) => {

        console.log(student);

    }
);

28. FlatList Data in React Native

React Native's FlatList commonly works with arrays of data.

import { FlatList } from "react-native";

const students: string[] = [
    "Amit",
    "Rahul",
    "Priya"
];

<FlatList
    data={students}
    renderItem={({
        item
    }) => (
        <Text>
            {item}
        </Text>
    )}
/>

Typing array data correctly becomes especially useful when working with larger React Native applications.

29. Common Array Type Mistakes

Mistake 1: Adding the wrong type of value.

let numbers: number[] = [
    10,
    20
];

// Error
// numbers.push("30");

Mistake 2: Assigning a string array to a number array.

let numbers: number[] = [
    10,
    20
];

// Error
// numbers = ["10", "20"];

Always make sure that array elements match the declared array type.

30. Array Type Summary

Array Type Example
Number array number[]
String array string[]
Boolean array boolean[]
Empty number array let nums: number[] = [];
Add item numbers.push(40)
Remove item numbers.pop()
Array length numbers.length
Check value numbers.includes(20)
Remember: TypeScript arrays allow you to define what type of values an array can contain. This provides better type safety and makes array-based applications easier to maintain.

📌 Key Points

  • An array stores multiple values in a single variable.
  • number[] creates an array of numbers.
  • string[] creates an array of strings.
  • boolean[] creates an array of Boolean values.
  • TypeScript can infer the type of an array automatically.
  • The push() method adds elements.
  • The pop() method removes the last element.
  • The length property returns the number of elements.
  • map() and filter() are useful for processing arrays.
  • Arrays are widely used for lists and API data in React Native applications.

🧠 Quick Quiz

Question: Which TypeScript syntax represents an array of numbers?