Typed Arrays
A free lesson from Swipe Coding · TypeScript, level 2 · updated October 2026
By the Swipe team, founded by Colin · how we make lessons
In JavaScript, an array can hold anything — numbers, strings, even a mix of both. TypeScript lets you lock an array down to a single element type, so the compiler can catch mistakes the moment you make them instead of when the code actually runs.
const scores: number[] = [10, 20, 30];
scores.push(40); // fine — 40 is a number
scores.push("oops"); // Error: Argument of type 'string' is not
// assignable to parameter of type 'number'.That same idea works for any type — string[] for an array of strings, boolean[] for an array of booleans, even Product[] for an array of your own custom objects. TypeScript also offers a second, equivalent syntax using the built-in Array<T> generic, where T is whatever type the array holds.
const names: Array<string> = ["Ada", "Grace", "Linus"]; // Identical meaning to: const sameNames: string[] = ["Ada", "Grace", "Linus"];
You don't always have to write the annotation yourself. If you initialize an array with values, TypeScript infers the element type from what's inside it — this is why scores.push("oops") failed above even without us writing number[] explicitly at that call site. Mixing types in one declared array is exactly what the annotation exists to prevent.
const mixed: number[] = [1, 2, "three"]; // Error: Type 'string' is not assignable to type 'number'. const ids = [101, 102, 103]; // inferred as number[] automatically — no annotation needed
const tags: string[] = ["a", "b", 3]; fails to compile because the array is annotated to hold only ____.
Which of these is equivalent to writing let values: number[] = [];?
A variable typed as boolean[] can have a string pushed onto it without a compiler error.
Keep your streak going in Swipe Coding
Swipe through every level of TypeScript on your iPhone — with XP, stars, streaks, a personal advisor and the full track beyond these free levels.
Get the Swipe apps →