typeof Type GuardProblem Statement'Write a function process(val: string | number) that returns val.length if it is a string, and val.toFixed(2) if it is a number.'Approach 1. Use `typeof val === 'string'` and `typeof val === 'number'`. Solution`function process(val: string | number) { if (typeof val === "string") { return val.length;
instanceof Type GuardProblem Statement'Write a function logDate(val: Date | string). If it is a Date, log the year. If string, log the value.'Approach 1. Use `val instanceof Date`. Solution`function logDate(val: Date | string) { if (val instanceof Date) { console.log(val.getFullYear());
The 'in' OperatorProblem Statement'Given Bird { fly: () => voidApproach 1. Check for the existence of a unique property using `"prop" in animal`. Solution`interface Bird { fly: () => void
Custom Type PredicateProblem Statement'Write a function isString(val: any): val is string that returns a boolean type predicate.'Approach 1. The return type must be `parameterName is Type`. Solution`function isString(val: any): val is string { return typeof val === "string";
Discriminated Union GuardProblem Statement'Narrow a Shape union (Circle | Square) using an "if" check on a common "kind" property.'Approach 1. Access `shape.kind`. Solution`type Circle = { kind: "circle", radius: number
Truthiness NarrowingProblem Statement'Write a function logMessage(msg: string | null). Narrow to string using a truthy check.'Approach 1. `if (msg)` narrows out null. Solution`function logMessage(msg: string | null) { if (msg) { console.log(msg.toUpperCase());
Falsy NarrowingProblem Statement'Write a function check(val?: number). Narrow it to show it is NOT undefined.'Approach 1. Use `if (val !== undefined)`. Solution`function check(val?: number) { if (val !== undefined) { return val * 2;
Equality NarrowingProblem Statement'Given two variables of type string | number, compare them using ===. What happens inside the if block?'Approach 1. If they are equal, they must have the same type. Solution`function compare(a: string | number, b: string | boolean) { if (a === b) { // Both must be strings here! a.toUpperCase(); b.toUpperCase();
Complex Type PredicateProblem Statement'Write a function isUser(obj: any): obj is User that checks if name (string) exists.'Approach 1. Check `obj && typeof obj.name === "string"`. Solution`interface User { name: string
Narrowing in CallbacksProblem Statement'Does narrowing persist inside a nested callback? Explain.'Approach 1. No, because the variable could have changed between the check and the callback execution. Solution`function check(x: string | null) { if (x !== null) { setTimeout(() => { // console.log(x.toUpperCase()); // Error: x could be null
Assertion FunctionsProblem Statement'Write an assertion function assertIsNumber(val: any): asserts val is number.'Approach 1. Use `asserts parameterName is Type`. Solution`function assertIsNumber(val: any): asserts val is number { if (typeof val !== "number") { throw new Error("Not a number!");
Array Filter GuardProblem Statement'Filter out nulls from an array [string | null] and ensure the result is string[].'Approach 1. Pass a type guard to the `.filter()` method. Solutionconst arr: (string | null)[] = ["a", null, "b"]; const filtered: string[] = arr.filter((item): item is string => item !== null);
Unknown NarrowingProblem Statement'Use unknown and narrow it to an object with properties.'Approach 1. Use `typeof val === "object"` and null checks. Solution`function handle(val: unknown) { if (val && typeof val === "object" && "id" in val) { console.log((val as any).id);
Switch True GuardProblem Statement'Can you use switch(true) for type narrowing? Show an example.'Approach 1. Yes, by putting `typeof` checks in the cases. Solution`function process(val: string | number) { switch(true) { case typeof val === "string": // val is string here return; case typeof val === "number": // val is number here return;
Custom Class GuardProblem Statement'Create a guard to distinguish between instances of class A and class B.'Approach 1. Use `instanceof`. Solution`class A { a = 1
Readonly NarrowingProblem Statement'Does narrowing work for readonly properties?'Approach 1. Yes, TS treats them like any other property for narrowing purposes. Solution`interface Props { readonly val?: string
Never Type GuardProblem Statement'Show how to use the "never" type to ensure exhaustive narrowing of an enum.'Approach 1. If the code reaches the default/else case, the type should be never. Solution`enum Status { A, B
Property Access GuardProblem Statement'Narrow an object using a property access check inside an if.'Approach 1. If `obj.prop` exists, TS narrows it. Solution`function check(obj: { name?: string
Not Operator NarrowingProblem Statement'How to narrow out a type using the ! operator?'Approach 1. `if (!val)` narrows the "else" branch to the type. Solution`function process(val: string | null) { if (!val) return; // val is string here
Switch DiscriminationProblem Statement'Use a switch statement to narrow a Union of { type: "A"Approach 1. Standard pattern for Redux-like actions. Solution`type Event = { type: "click"