0
0
Typescriptprogramming~20 mins

Equality narrowing in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Equality Narrowing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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");
ANumber 42 detected\nOther value
BOther value\nNumber 42 detected
CNumber 42 detected\nNumber 42 detected
DOther value\nOther value
Attempts:
2 left
💡 Hint
Think about how strict equality (===) works with different types in TypeScript.
Predict Output
intermediate
2: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");
AError at runtime\nHELLO
BValue is null or undefined\nhello
CValue is null or undefined\nHELLO
DValue is null or undefined\nError at runtime
Attempts:
2 left
💡 Hint
Remember that == null checks for both null and undefined.
🔧 Debug
advanced
2: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);
ARuntime error because number 10 has no 'length' property
BSyntax error due to missing type annotation
CNo error, prints 'No value'
DRuntime error because null is accessed
Attempts:
2 left
💡 Hint
Check what properties exist on numbers versus strings.
📝 Syntax
advanced
1: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?
Aif (val == true) { console.log("It's true"); }
Bif (val === true) { console.log("It's true"); }
Cif (val) { console.log("It's true"); }
Dif (val === 'true') { console.log("It's true"); }
Attempts:
2 left
💡 Hint
Strict equality checks both type and value.
🚀 Application
expert
2: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);
A1
B3
C4
D2
Attempts:
2 left
💡 Hint
Count items that are not null, not zero, and not empty string.