

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Still calibrating
click for more info
Not enough gems
Cost: 6 gems
1: Narrowing Types
incomplete
2: Unknown Type
incomplete
3: Type Hierarchy
incomplete
4: Narrowing Using In
incomplete
5: Type Predicates
incomplete
6: Exhaustive Checks
incomplete
7: Guard Clauses
incomplete
8: Type Assertion
incomplete
9: Double Assertion
incomplete
10: Non-Null Assertion
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
If you've ever heard a Rust enjoyer (and let's be honest, if you know one, you've heard from them) talk about how great the Rust programming language is, you've probably heard them mention "pattern matching" and "exhaustive checks".
To be fair, it's a pretty cool idea. Say we have this union type:
type Notif = "email" | "sms" | "push";
and we have this function that uses it:
function sendNotification(notif: Notif) {
switch (notif) {
case "email":
return "Sending email";
case "sms":
return "Sending SMS";
case "push":
return "Sending push notification";
}
return "Unknown notification type";
}
This might be a very reasonable way to write JavaScript code, but that final return "Unknown notification type"; is actually redundant in good TypeScript code. The switch statement is exhaustive, and TypeScript is smart enough to know that return "Unknown notification type"; is actually unreachable code, and will give us a compiler error (assuming we have configured tsc to do so)!
Design your types so that you get these kinds of useful errors.
The incrementCount function is working as intended at runtime, but someone committed an unnecessary default case that throws an error.
Remove the unnecessary code.