Published lesson · 30 minutes

Work with arrays and reusable shapes

Objective: Describe an array of records with an interface and derive a summary without losing the record types.

Understand the idea

Work with arrays and reusable shapes works best when the learner can explain the reason behind each step, not just copy a result. An interface describes the fields a record must contain; an array type applies that shape consistently to every item in a collection.

A school timetable stores period, subject and room for each session, then groups or displays the sessions in order. Treat the names and details as invented practice information; the example is for learning a method and does not describe a live service.

A dependable sequence is: Define an interface, type the collection, and write a function that accepts the array and returns a typed summary. Write down the important decision before you begin so another person can follow the reasoning.

Test an empty collection and one record. Let the compiler flag a missing room instead of hiding it with `any`. Compare the result with the objective, then change one thing at a time if the first attempt does not work.

Add one timetable record and confirm the summary function still has a clear return type. Explain one assumption that would need checking before anyone used a similar result outside the classroom.

Keep a short record of the choices you made and the evidence you used to check them. A useful practice result is understandable by another learner, stays within the stated scope and makes its limitations visible before anyone relies on it.

Worked examples

Work with arrays and reusable shapes: practice example

Explore how work with arrays and reusable shapes supports the lesson objective with a small synthetic example.

main.ts

interface Session { period: number; subject: string; room?: string }
function describe(sessions: Session[]): string[] { return sessions.map((session) => `${session.period}: ${session.subject}`); }
console.log(describe([{ period: 1, subject: "HTML" }]));

Practice tasks

  1. Explore work with arrays and reusable shapes

    Use the worked example for “Work with arrays and reusable shapes”. Change one small detail to suit a different synthetic community situation, then explain what you expected and how you checked whether the change still met the lesson objective.

  2. Build a TypeScript practice example

    Create a small practice result that demonstrates this objective: Describe an array of records with an interface and derive a summary without losing the record types. Use only synthetic information, keep the first version focused, and write down one limitation that a later version should address.

  3. Review and explain the result

    Review your “Work with arrays and reusable shapes” result as if another learner will use it without your explanation. Identify one strength, one exact point that may be unclear, and a revision that would make the result easier to understand or use.

Work with arrays and reusable shapes becomes easier when you define an interface, check the result and explain the choice to another learner.

Course overview