Challenge - 5 Problems
Equality Narrowing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this TypeScript code using equality narrowing?
Consider the following TypeScript code that uses equality narrowing with a union type. What will be printed to the console?
Typescript
function checkValue(x: string | number) { if (x === 42) { console.log("Number 42 detected"); } else { console.log("Other value"); } } checkValue(42); checkValue("42");
Attempts:
2 left
💡 Hint
Think about how strict equality (===) works with different types in TypeScript.
✗ Incorrect
The check x === 42 only passes if x is the number 42. When x is the string "42", the condition fails, so the else branch runs.
❓ Predict Output
intermediate2:00remaining
What does this TypeScript code print when using equality narrowing with null?
Look at this TypeScript function that narrows a value using equality checks. What will be the output?
Typescript
function processValue(val: string | null) { if (val == null) { console.log("Value is null or undefined"); } else { console.log(val.toUpperCase()); } } processValue(null); processValue("hello");
Attempts:
2 left
💡 Hint
Remember that == null checks for both null and undefined.
✗ Incorrect
The first call passes null, so the first branch runs. The second call passes a string, so the else branch runs and prints the uppercase string.
🔧 Debug
advanced2:30remaining
Why does this TypeScript code cause a runtime error despite equality narrowing?
Examine the code below. It uses equality narrowing but causes a runtime error. What is the cause?
Typescript
function printLength(x: string | number | null) { if (x !== null) { if (x === 42) { console.log("The answer"); } else { console.log(x.length); } } else { console.log("No value"); } } printLength(10);
Attempts:
2 left
💡 Hint
Check what properties exist on numbers versus strings.
✗ Incorrect
The else branch tries to access x.length, but x can be a number (like 10) which has no length property, causing a runtime error.
📝 Syntax
advanced1:30remaining
Which option correctly uses equality narrowing to check for a boolean value?
You want to run code only if a variable is exactly the boolean true. Which code snippet correctly uses equality narrowing?
Attempts:
2 left
💡 Hint
Strict equality checks both type and value.
✗ Incorrect
Only option B uses strict equality to check that val is exactly the boolean true, not just truthy or a string.
🚀 Application
expert2:30remaining
How many items are in the resulting array after filtering with equality narrowing?
Given this TypeScript code that filters an array using equality narrowing, how many items remain in the filtered array?
Typescript
const mixed: (string | number | null)[] = [null, 0, "", 42, "hello"]; const filtered = mixed.filter(x => x != null && x !== 0 && x !== ""); console.log(filtered.length);
Attempts:
2 left
💡 Hint
Count items that are not null, not zero, and not empty string.
✗ Incorrect
The filtered array excludes null, 0, and "". Remaining items are 42 and "hello", so length is 2.