in, instanceof, discriminated unions
Use in/property checks, instanceof for classes, and discriminated unions to branch safely.
in, instanceof, discriminated unions is a free TypeScript Academy lesson on CoddyKit — lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Intro
Goal: Practice three powerful guards: in for property checks, instanceof for classes, and discriminated unions for clean branching.
in operator
in narrows by testing a property name at runtime. Great for object-literal unions.
type HasLength = { length: number };
function print(v: string | HasLength) {
if ("length" in v) {
// v is HasLength
console.log("len:", v.length);
} else {
// v is string
console.log(v.toUpperCase());
}
}
print({ length: 3 });
print("ts");instanceof
instanceof checks prototypes for class-based values. Works when the constructor is available at runtime.
class Person {
constructor(public name: string) {}
}
function hello(x: Person | Date) {
if (x instanceof Person) {
console.log("Hi", x.name);
} else {
console.log("Year", x.getFullYear());
}
}
hello(new Person("Ada"));
hello(new Date(2024, 0, 1));Discriminated union
Discriminated unions use a literal tag (like kind) to switch cleanly with full narrowing per case.
type Circle = { kind: "circle"; radius: number };
type Square = { kind: "square"; size: number };
type Shape = Circle | Square;
function area(s: Shape): number {
switch (s.kind) {
case "circle":
return Math.PI * s.radius ** 2;
case "square":
return s.size * s.size;
}
}
console.log(area({ kind: "circle", radius: 2 }));
console.log(area({ kind: "square", size: 3 }));Combine guards
Combine tag checks with in to narrow nested shapes and access only safe members.
type FetchOk = { status: "ok"; data: { length: number } };
type FetchErr = { status: "error"; message: string };
type Result = FetchOk | FetchErr;
function handle(r: Result) {
if (r.status === "ok" && "length" in r.data) {
console.log("items:", r.data.length);
} else {
console.log("error:", r.message);
}
}
handle({ status: "ok", data: { length: 5 } });
handle({ status: "error", message: "oops" });Guidelines
Guidelines:
- Use in for property-based unions.
- Use instanceof with classes.
- Use discriminated unions for clear control flow.
in operator check
Quick check: Which operator checks a property at runtime and narrows types?
Recap
Recap: in tests properties, instanceof checks classes, and discriminated unions give clean, type-safe switches.
Frequently asked questions
Is the “in, instanceof, discriminated unions” lesson free?
Yes — the full text of “in, instanceof, discriminated unions” is free to read here on the web, and the TypeScript Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.
What will I learn in “in, instanceof, discriminated unions”?
Use in/property checks, instanceof for classes, and discriminated unions to branch safely. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start TypeScript Academy?
No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “in, instanceof, discriminated unions” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this TypeScript Academy lesson?
Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- typeof, equality, truthiness narrowing
- in, instanceof, discriminated unions
- Exhaustiveness with never (intro)