Menu
CoddyTech

Typed Records

Part of the Fundamentals section of Coddy's Angular journey. Lesson 36 of 55.

An interface describes the shape of a record. The example gives each parcel a stable ID, a label and a numeric weight. An array annotation states that every entry should have that shape.

interface Parcel {id:number; label:string; weight:number}
parcels:Parcel[]=[{id:4,label:"Box",weight:3},{id:9,label:"Bag",weight:2}];
// <p>{{parcels[1].label}}</p>

The browser still receives ordinary JavaScript objects. Interfaces disappear during transpilation and do not validate external data. Here the records are supplied locally, so use their fields consistently when deriving a view.

For a numeric summary, JavaScript reduce carries an accumulator through the array. Starting at 0, add each record's weight: the weights 3 and 2 produce 5. The initial zero also makes an empty array total zero.

parcels.reduce((sum, parcel) => sum + parcel.weight, 0)
challenge icon

Challenge

Medium

Render the supplied typed parcels in summary as label, colon, weight, semicolon for each item, with no extra separator. Use each record's ID for tracking.

Edit the supplied files. Press Run preview to refresh the preview after edits, then use Check or Run code to grade your work. Keep the requested element IDs.

Try it yourself

<!doctype html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1"><link rel="stylesheet" href="styles.css"></head><body><app-root></app-root><script type="module" src="main.ts"></script></body></html>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals