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.
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?: stringmay be missing (its type is thenstring | undefined). readonlymeans 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 typeCitycan 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.anyturns 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…
Ngozi: “The partner’s feed sends weights as text and we cannot change them. Does putting the right type on the interface fix that? What does the compiler know about data that comes from outside the program?”
🔒 The debrief unlocks when you close the case. Finish the lab above.
The debrief
Ngozi: “So the compiler cannot see the outside world. It only checks that our code agrees with itself. But now the contract is written down.”
What a senior developer would do next. Treat the interface as documentation that is checked. Keep types close to the data they describe, export them from one place, and make them as narrow as the business rule: a union of cities, not a string. Remember the limit the twist found: types are erased at run time, so data from a file, a request or a partner has to be checked when it arrives (that is the Trust Nothing case).
The traps. Using any or a bare string because it is quicker; marking everything optional so nothing ever fails; and believing the compiler guards data that came from outside.
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).
🧪 Want to practise this on your own computer? See how to set up your lab →