TypeScript for real code · Chapter 6 of 36
This or that
By the end of this chapter you can type a value that can be one of several things, and check which one you have before you use it. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Why types
- 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 has four bugs from one week of orders. The quantity box sends a number from the phone app but text like ' 3 ' from the counter tablet. A guest who checks out without signing in has no name. A payment is UPI or cash, and each has different fields. Stock that nobody has counted yet is empty, which is not the same as sold out.
With the types you know so far, a parameter is one thing. Type the quantity as number and every call from the tablet is an error; type it as string and the phone app breaks. Bunty typed it any and called input.trim(), and the app crashed on the first number: any lets every mistake through.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
readQty: a quantity as a number or as text
The phone app sends the quantity as a number, and the counter tablet sends it as text with spaces around it. readQty(2) is 2 and readQty(' 3 ') is 3.
Give input the type string | number instead of any, so a list or null is a type error. It returns a number. Only text has trim, so find out which one you have before you call it.
- Task 2 · interview question
Interview: greeting, greet guests too
The welcome screen shows the customer's name in capitals. A guest has no name, so their name is null. greeting('Anjali') is 'NAMASTE, ANJALI' and greeting(null) is 'NAMASTE, GUEST'.
Keep the parameter as string | null and return a string. Handle null with a real check, not with a ! after name: that only silences the compiler, and greeting(null) still crashes. Dev will ask what the error means and why ! is not a fix.
- Task 3
paymentNote: tell UPI and cash apart
A UPI payment has an upiId, and a cash payment has tendered, the amount handed over. paymentNote({ upiId: 'anjali@okaxis' }, 65) is 'UPI from anjali@okaxis', and paymentNote({ tendered: 100 }, 65) is 'Cash, give back ₹35' (tendered minus the bill).
Give payment the type Upi | Cash (both types are exported in the file) instead of any, so a voucher, or tendered written as text, is a type error. Tell the two apart in a way TypeScript understands.
- Task 4
stockLabel: sold out is not the same as not counted
null means nobody has counted the stock yet, and 0 means sold out. stockLabel(null) is 'not counted yet', stockLabel(0) is 'sold out' and stockLabel(12) is '12 left'.
Keep the types: stock is a number or null, and the result is a string. Bunty's first check treats 0 like null: fix it, so 0 reaches the sold-out line.
What you will learn
- Union types: string | number means "one of these", and TypeScript lets you use only what all of them have.
- Narrowing: a typeof, equality or in check in an if, after which TypeScript knows which one you have.
- The truthiness trap: why if (!x) throws away 0 along with null.
More in Why types
Build "This or that" 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.