Course contentsShow
← TypeScript
Lesson 106 of 1,7963. Basic TypesFree lesson

Empty Array Initialization

Why empty arrays get type never[] and how to provide explicit type annotations when needed.

Empty Array Initialization

You'll learn why TypeScript treats empty arrays specially and how to tell it what type you mean to store.

The Empty Array Problem

When you write const items = []; in TypeScript, the compiler faces a dilemma: what type should this array be? There's no data to infer from!

TypeScript's solution is conservative—it assigns the type never[]. The never type represents values that can never occur, meaning TypeScript thinks this array will never hold anything. This prevents you from accidentally pushing values later:

const items = []; // Type: never[]
items.push(42);   // Error: Argument of type 'number' is not assignable to parameter of type 'never'

Providing Explicit Type Annotations

To create an empty array that can hold values, you must tell TypeScript what type you intend to store using a type annotation:

const numbers: number[] = [];
numbers.push(42);    // ✓ Works fine

const names: string[] = [];
names.push("Alice"); // ✓ Works fine

Think of it like labeling an empty box: TypeScript won't know if your box is for books, toys, or tools unless you write it on the label.

Why This Matters

This behavior protects you from mistakes. Without explicit types, TypeScript can't verify what you plan to put in the array later. The type annotation serves as your declaration of intent—it lets both TypeScript and other developers know what belongs in that array.

Key Takeaway: Empty arrays get type never[] by default, so always provide an explicit type annotation like const items: number[] = []; to specify what the array will hold.