Unions and optional values
You'll learn to read | (one of several types), ? (a field that may be missing), and why TypeScript makes you check for undefined.
find that finds nothing. Most crashes in apps come from that, and these types point at it.Watch & read
Start with the required ones. The others are there when you need them.
Explanation
An order's payment can be pending, paid or failed. Nothing else. And some customers give a phone number, some don't. You want the types to say both things, so TypeScript can check every place that uses them.
The | means "or". A type made with | is called a union type.
type Status = "pending" | "paid" | "failed";let code: string | number = "A12";
Status allows exactly three strings. A single exact value used as a type, like "paid", is called a
literal type. If you write "shipped", you get Type '"shipped"' is not assignable to type 'Status'.
It is like the options on a bKash screen: you can only tap the buttons that are there.
A ? after a field name means the field may be missing. It is an optional property.
type Order = {id: number;status: Status;phone?: string;};
phone?: string means phone is a string or undefined. So when you use it, TypeScript stops you:
order.phone.length// 'order.phone' is possibly 'undefined'.
In plain JavaScript, this line crashes with Cannot read properties of undefined the first time a
customer has no phone. TypeScript shows it before that day comes. The fix is to check first:
if (order.phone === undefined) {return "No phone";}return order.phone.slice(0, 3); // safe: phone is a string here
After the if, TypeScript knows phone can't be undefined any more. This is called narrowing: a
check makes the type smaller. You'll see the same thing with find, which gives back
Student | undefined, because it may find nothing.
People often (and AI very often) make the red line go away by adding !, as in order.phone!.length.
That only tells TypeScript "trust me". It doesn't check anything, and the app still crashes when phone
is missing. Add a real check instead.
When you read a type, look for | and ?. Each one means "this might be something else", so look for
the if that handles the other case.
Code tour
Click a line number with a ring to read the note for that line.
Practice
Short drills. Take your time and read the code before you answer.
1type Order = { id: number; phone?: string };2function digits(order: Order) {3 return order.phone.length;4}
1type Status = "pending" | "paid" | "failed";2let payment: Status = "pending";3payment = "paid";4payment = "shipped";
phone?: string would make order.phone.slice(...) show 'order.phone' is possibly 'undefined'. This is plain JavaScript, so you'll see the crash instead. phonePrefix should give the first 3 digits of the phone, or "none" when there is no phone. Fix it so all three checks pass.Quick check
3 questions. Each one explains itself after you answer.
- 1 / 3In
type Customer = { name: string; phone?: string }, what canphonebe? - 2 / 3
const found = students.find(s => s.name === "Nusrat");IfstudentsisStudent[], what is the type offound? - 3 / 3AI changes
order.phone.lengthtoorder.phone!.lengthand the red line is gone. What happens when a customer has no phone?
Explain it back
How confident are you with Unions and optional values?
Done
status: "pending" | "paid" and phone?: string, and fix code that TypeScript says is possibly 'undefined'.