Common Array Type Pitfalls
You'll learn: How to avoid the most common mistakes when working with array methods that can confuse TypeScript's type system.
The Problem with Array Mutation
When working with arrays in TypeScript, beginners often stumble over methods that modify the original array versus those that return a new one. TypeScript expects you to understand this difference, or you'll run into confusing type errors.
push() vs concat(): Two Different Behaviors
The push() method mutates the original array and returns a number (the new length), not the array itself:
const numbers: number[] = [1, 2, 3];
const result = numbers.push(4);
// result is type 'number' (value: 4), NOT number[]
If you try to chain array methods on push(), TypeScript will complain because you're working with a number, not an array.
The concat() method, however, returns a new array without changing the original:
const numbers: number[] = [1, 2, 3];
const result = numbers.concat(4);
// result is type 'number[]', original unchanged
Think Like This
Imagine push() as someone telling you "I added it—here's how many items are in the box now" (just a count). But concat() says "here's a new box with everything combined" (a whole new array).
Watch Out for Type Widening
When you initialize an empty array without a type annotation, TypeScript infers any[] if it can't figure out what you'll add later. Always annotate empty arrays:
const items: string[] = []; // Good
items.push("hello");
Key Takeaway: Remember that push() returns a number and mutates the array, while concat() returns a new array—misunderstanding this distinction causes most array type errors for beginners.