Empty Array Inference Problems
You'll learn why TypeScript can't always guess the right type for an empty array and how to fix it.
When you write const items = [], TypeScript faces a dilemma: what type should items hold? Unlike [1, 2, 3] where it can infer number[], an empty array provides zero clues about its intended contents.
The Inference Lottery
Depending on your tsconfig.json settings and context, TypeScript will infer an empty array as either:
never[](strict mode) — an array that can never hold anythingany[](loose mode) — an array that can hold anything
Both create problems:
// With strict settings
const items = []; // Type: never[]
items.push(1); // Error: Argument of type 'number' not assignable to 'never'
// Without strict settings
const items = []; // Type: any[]
items.push(1);
items.push("text");
items.push(true); // Accepts everything — no safety!
The Fix: Be Explicit
Always annotate empty arrays with their intended type:
const numbers: number[] = [];
numbers.push(42); // Works perfectly
const names: string[] = [];
names.push("Alice"); // Type-safe
Think of it like labeling an empty box: if you write "Books" on the outside, everyone knows what belongs inside. Without a label, people might put anything in there — or refuse to use it at all.
Key Takeaway: Empty arrays cannot be inferred reliably; always provide an explicit type annotation like const items: T[] = [] to avoid never[] or any[] problems.