Arrays

`T[]` or `Array<T>` — Pick One and Be Consistent

Arrays

Two equivalent ways to type an array of T. `readonly` and generic helpers add safety.

3 min read Level 1/5 #typescript#arrays#readonly
What you'll learn
  • Annotate arrays
  • Use `readonly` for arrays that shouldn't change
  • Recognize the generic form

An array of Ts is T[] or Array<T>. Both are identical — pick one style for your codebase.

The Two Forms

const tags: string[] = ["js", "ts", "react"];
const tags: Array<string> = ["js", "ts", "react"];     // same

const scores: number[] = [10, 9, 7];
const scores: Array<number> = [10, 9, 7];               // same

T[] is more common. Array<T> reads better for complex types:

let chunks: (string | number)[] = [];   // ✓ works
let chunks: Array<string | number> = []; // ✓ reads slightly nicer for this case

Empty Arrays

const items: string[] = [];

const items = [];   // inferred as `any[]` — usually NOT what you want

When you start with an empty array and push to it later, annotate explicitly. Otherwise TS infers never[] or any[].

readonly — Immutable Arrays

const tags: readonly string[] = ["js", "ts"];

tags.push("react");
//   ~~~~ Property 'push' does not exist on type 'readonly string[]'.

tags[0] = "rust";
// ~~~ Index signature in type 'readonly string[]' only permits reading.

readonly removes mutating methods (push, pop, splice, sort, etc.) from the type. The runtime value is still a normal array; the type system just refuses to let you mutate it.

Useful for function parameters — promise you won’t mutate:

function sum(nums: readonly number[]): number {
  return nums.reduce((s, n) => s + n, 0);
}

Generic Form for Other Iterables

The generic form is everywhere:

const set: Set<string> = new Set(["a", "b"]);
const map: Map<string, number> = new Map();
const list: Array<{ id: string }> = [];

Mixed Arrays

Not type-safe — but possible:

const mixed: (string | number)[] = ["a", 1, "b", 2];

For arrays of varied SHAPES, use a tuple (next lesson) or a discriminated union.

Up Next

Tuples — when an array has a known length and known types at each index.

Tuples →