TypeScript · module 3 of 21

Arrays & Tuples

Arrays hold multiple values of the same type — TypeScript enforces this at compile time.

Course map

What this module covers

4 steps · TypeScript

  1. Step 1. Arrays & Tuples

    Arrays hold multiple values of the same type — TypeScript enforces this at compile time. let scores: number[] = [95, 87, 100]; let flags: Array<boolean> = [true, false]; let mixed: (string | number)[] = ["hello", 42]; Array methods stay type-safe: scores.map(s => s * 2), scores.filter(s => s >= 90). Tuples are fixed-length arrays where each position has its own type: let user: [string, number] = ["Alice", 25]; let [name, age] = user; // destructuring readonly arrays and tuples cannot be modified after creation — use them for data that should never change. Array: any length, all elements same type. Tuple: fixed length, each position typed individually. Use tuples for structured data like coordinates or CSV rows.

    Tuples let you express "exactly these types, in exactly this order" — something a plain array type cannot do. In TypeScript, make the state/interaction visible in the UI so completion is easy to verify.

    Check yourself: [TypeScript] Add a tuple variable with two number positions.

  2. Step 2. Quiz: Arrays & Tuples

    Answer these questions about arrays and tuples.

    Arrays hold same-typed values of any length; tuples fix both the length and the type at each position. In TypeScript, make the state/interaction visible in the UI so completion is easy to verify.

    Check yourself: [TypeScript] Answer the quiz below.

  3. Step 3. Build Typed Arrays & Tuples

    Create arrays and tuples with proper types: fruits as string[] with at least 3 fruit names, numbers as number[] with at least 3 numbers, person as a [string, number] tuple with a name and age, colors as a readonly string[] with 3 color names.

    Combining arrays and tuples in one exercise shows exactly when each is the right tool: arrays for open-ended lists, tuples for fixed structured records. In TypeScript, make the state/interaction visible in the UI so completion is easy to verify.

    Check yourself: [TypeScript] Do fruits, numbers, person, and colors all use the correct array/tuple types?

  4. Step 4. Mini project: Arrays & Tuples

    Create arrays and tuples with proper types: fruits as string[] with at least 3 fruit names, numbers as number[] with at least 3 numbers, person as a [string, number] tuple with a name and age, colors as a readonly string[] with 3 color names. Turn the completed challenge into a small standalone project. Add realistic content, clear naming, one edge case or error state, and a short README-style explanation of how the main idea works.

    Combining arrays and tuples in one exercise shows exactly when each is the right tool: arrays for open-ended lists, tuples for fixed structured records. This project stage asks you to apply the same idea without step-by-step scaffolding. In TypeScript, make the state/interaction visible in the UI so completion is easy to verify.

    Check yourself: [TypeScript] Do fruits, numbers, person, and colors all use the correct array/tuple types?

Loading code lab...