TypeScript for real code · Chapter 7 of 36
Naming types
By the end of this chapter you can give an object's shape a name, build new types from it, and choose between a type alias and an interface. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Modelling Tapri
- 4 tasks
- TypeScript
- In the all labs plan
The first 2 chapters of TypeScript for real code are free. This one comes with the all labs plan.
Why you need this
Ravi found the shape of an address written out in full twice in the delivery code, and the two copies disagree: one says a pincode is text, the other says it is a number. Bunty gave up on customers and wrote type Customer = any, so a typo in a field name compiles and prints undefined on a customer's screen. And riders, who are customers with a vehicle, cannot be created at all.
So far you have written every object type in full where it is used. The second time, you copy it, and nothing tells you the copies mean the same shape when they drift apart.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
Customer: a real shape instead of any
Bunty typed Customer as any, so anything goes. Give it the real shape: a name and a phone, both strings. An interface or a type alias both work. welcome({ name: 'Anjali', phone: '98450 12345' }) is 'Welcome back, Anjali'.
Customer must not be any. A customer with no phone is a type error, and so is a phone written as a number, like 9845012345: phone numbers are kept as text.
- Task 2
LoyalCustomer: a customer with points
A loyal customer is a customer with points, a number. rewardLine({ name: 'Anjali', phone: '98450 12345', points: 120 }) is 'Anjali has 120 points'.
Build LoyalCustomer from Customer instead of copying the fields, so a loyal customer is still a customer: it can be used where a Customer is expected, and one with no phone or with no points is a type error.
- Task 3
Address: one shape for both address functions
The address shape is written out twice, and the two copies disagree about the pincode. Export one type, Address, with street, area and pincode, all strings, and use it as the parameter type of both formatAddress and deliveryZone.
For home = { street: '12th Main', area: 'Indiranagar', pincode: '560038' }, formatAddress(home) is '12th Main, Indiranagar 560038' and deliveryZone(home) is 'near'. Pincodes below 560040 are near and the rest far, so deliveryZone gives 'far' for pincode '560100'. A pincode written as a number is a type error, and so is an address with no street.
- Task 4 · interview question
Interview: Rider, whose phone became never
A rider is a customer with a vehicle, 'cycle' or 'scooter'. Bunty built Rider with an intersection and gave phone a second type, a number, so the phone became never and no rider can be created. riderCard({ name: 'Arjun', phone: '99000 11223', vehicle: 'scooter' }) is 'Arjun (scooter), 99000 11223'.
Fix Rider so its phone is a string, like every customer's, and keep it built from Customer, so a rider can be used where a Customer is expected. A vehicle like 'bus' is a type error, and so is a rider with no vehicle. Dev will ask why nobody could create a rider, and which of extends or the intersection would have caught it when Bunty wrote it.
What you will learn
- Type aliases and interfaces: two ways to give a shape one name, to read in one place and change in one place.
- Building on a type: extends and & add fields to a type you already have.
- Where they differ: & with a clashing field makes it never, and interfaces with the same name merge.
More in Modelling Tapri
Build "Naming types" yourself
Tapri’s codebase grows, and types keep it honest. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of TypeScript for real code are free. This one comes with the all labs plan.