Swipe
SwipeSwipe
Swipe Apps

Paths

Motivation

Information

Arrays & Tuples · Lesson 7 of 12

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

🤖
Arrays are a free-for-all in plain JS — TypeScript adds guardrails so mixed-up types get caught early. 📦

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'.
The number[] annotation means "an array where every element is a number." Pushing a string breaks that promise, so TypeScript refuses to compile it.

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"];
string[] and Array<string> mean exactly the same thing. Most TypeScript code favors the shorter T[] form, but you will see Array<T> in the wild too.
🤖
Same array-safety idea, just proven by the compiler working backward from your values. 🔄

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
Declaring the array as number[] up front means every element inside the literal has to match — TypeScript checks the whole list, not just the first value.
Fill in the blank

const tags: string[] = ["a", "b", 3]; fails to compile because the array is annotated to hold only ____.

Quick check

Which of these is equivalent to writing let values: number[] = [];?

True or false

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 →