Concept
A type guard is a runtime check TypeScript understands: typeof for primitives, in for properties, instanceof for classes. After the check, the variable has the narrower type inside that branch.
The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.
A few lines that show typeof and in guards doing its job.
function describe(value: string | number | Date) {
if (typeof value === "string") return value.toUpperCase();
if (value instanceof Date) return value.toISOString();
return value.toFixed(1); // number here
}
type Cat = { meow(): void }; type Dog = { bark(): void };
function speak(pet: Cat | Dog) {
"meow" in pet ? pet.meow() : pet.bark();
}Expecting typeof to tell null from an object. typeof null is "object", so check value === null first, or use a truthiness check when null and undefined are both possible.
Your roadmap puts these before typeof and in guards, unless you already know them.
Once a session shows you understand typeof and in guards, it comes back for review. Each review takes a minute and tells the memory model how well you still know it.
Topics that build on typeof and in guards.