TypeScript Developer

Case File: The Loose Object

Describe a parcel exactly with an interface: a readonly id, a number weight, a city from a fixed set and an optional note. The real TypeScript compiler checks your file in strict mode, accepts what is valid and must reject what is not. Names and fields are generated for you alone.

55 min

Sabi Logistics · Typed 1

The Loose Object

The dispatch screen crashed because a parcel's weight arrived as text, and the compiler said nothing

Ngozi Lead developer · Phone call

Ngozi. The dispatch screen crashed this morning. A parcel’s weight arrived as the text “2.5” instead of the number 2.5, and the function that adds up the weights glued the texts together instead of adding. Nothing in our code complained. We found out when a driver was told a van was carrying “02.51.5” kilograms.

I have wanted us to move to TypeScript for a year. This is the argument I needed. Here is the file where it starts: a Parcel, a function to total the weights and one to print a label. The types are wrong, and half of them are missing. Please make the types describe a parcel exactly, so that the compiler refuses what should never exist and still accepts everything that should.

The brief

What you need to know first.

  • TypeScript is JavaScript with types. A type says what kind of value something is. The compiler checks every use against the type before the program runs and throws away the types afterwards, so the code that ships is plain JavaScript. A mismatch such as adding a number to text is a compile error, caught at your desk, not in front of a driver.
  • An interface (or a type alias) describes the shape of an object:
interface Parcel {
  readonly id: string;   // text, and it can never be reassigned
  weight: number;        // required, a number
  note?: string;         // the ? makes it optional
}
  • A field without a question mark is required: leaving it out is an error. note?: string may be missing (its type is then string | undefined).
  • readonly means the compiler refuses to let anyone assign to the field after the object is made (error TS2540).
  • Putting a string where a number is expected is TS2322: “Type ‘string’ is not assignable to type ‘number’”.
  • A literal type is exactly one value, and a union (|) means “one of these”: type City = 'Lagos' | 'Kano' | 'Abuja'. A variable of type City can hold only those three texts, so a typo like 'Lagoss' becomes a compile error instead of a bug.
  • Strict mode ("strict": true) switches on the checks that matter most. The labs use it, as every real project should.
  • Do not escape with any. any turns the checker off for that value, which is the opposite of what you want.
  • How the lab checks you. It does not compare your spelling with an answer. It asks the real compiler: does your file compile? Is a valid parcel accepted, with and without a note? Is a string weight refused? A city outside the three? A missing weight? An assignment to id? And do the functions give the right answers?

The starter is generated for you, with your own field names. When every check passes the lab shows a reference starting with GT{. Submit it at the bottom of the page.

Preparing your code…

🔒 The debrief unlocks when you close the case. Finish the lab above.

Write the pull request description

Write the description for the pull request that fixes the Parcel type: what each field and its type now say (readonly, optional, the city union and the number weight), which mistakes the compiler now refuses (give examples), why you did not use any, and what the compiler still cannot protect against (data that arrives from outside).

0 words

🧪 Want to practise this on your own computer? See how to set up your lab →

📝

Knowledge Check

1 of 3

What does readonly do on an interface field?

Report a problem

About: /lessons/case-the-loose-object