← Back to TypeScript Course | Chapter 2: Basic Types | Lesson 2 of 11

Array Types

An array stores multiple values in a single variable. TypeScript lets you specify the type of values that an array can contain.
Syntax
typescript
let items: type[] = [value1, value2];
let items2: Array<type> = [value1, value2];

Basic Array Syntax

You can declare an array using the type[] syntax, such as string[] for an array of strings or number[] for an array of numbers. Every value pushed into or stored in that array must match the specified type, or the compiler flags it.

Example: Basic Array Syntax

typescript
let names: string[] = ["Amit", "Priya"];
let nums: number[] = [1, 2, 3];
console.log(names, nums);

Array Methods

Typed arrays support every normal JavaScript array method, like push, pop, map, and filter, exactly as before. The difference is that TypeScript checks the values passed to those methods, so pushing a number into a string[] array is caught immediately.

Example: Array Methods

typescript
let nums: number[] = [1, 2, 3];
nums.push(4);
// nums.push("five"); // rejected: string is not a number
console.log(nums.map(n => n * 2));

Alternative Array Syntax

TypeScript also supports the generic Array<Type> syntax as an alternative to Type[], and the two mean exactly the same thing. The generic form is sometimes preferred for readability when the element type itself is already a complex generic type.

Example: Alternative Array Syntax

typescript
let names: Array<string> = ["Kabir", "Neha"];
console.log(names);

Readonly Arrays

A readonly array, written as readonly Type[], allows values to be read normally but blocks any method that would mutate the array, such as push or splice.

This is useful for function parameters where you want to guarantee the caller's array isn't accidentally modified.

Example: Readonly Arrays

typescript
let ids: readonly number[] = [1, 2, 3];
// ids.push(4); // rejected: readonly arrays block mutation
console.log(ids);

Arrays of Objects

Arrays can contain objects with a defined shape, where an interface or object type describes the properties every item in the array must have. This lets TypeScript catch a typo in a property name on any single object inside a large array of them.

Example: Arrays of Objects

typescript
interface Product {
  name: string;
  price: number;
}
let products: Product[] = [{ name: "Pen", price: 2 }, { name: "Book", price: 8 }];
console.log(products);
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Writing let nums: number[] = [1, 2, "3"], mixing types that the array type does not allow.
  2. Declaring let items = [] without a type and getting an implicit any[] instead of string[] or number[].
  3. Calling push on a readonly number[], which is a compile error because the array is read-only.
🔒

Chapter Quiz — Complete all 11 topics to unlock

0/11 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.