0
0
Typescriptprogramming~20 mins

The in operator narrowing in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
In Operator Narrowing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of type narrowing with 'in' operator
What is the output of this TypeScript code when run with Node.js (ignoring type errors)?
Typescript
type Fish = { swim: () => string };
type Bird = { fly: () => string };

function move(animal: Fish | Bird) {
  if ('swim' in animal) {
    return animal.swim();
  } else {
    return animal.fly();
  }
}

const fish: Fish = { swim: () => "Swimming" };
const bird: Bird = { fly: () => "Flying" };

console.log(move(fish));
console.log(move(bird));
ARuntime error: animal.swim is not a function
B"Flying" followed by "Swimming"
C"Swimming" followed by "Flying"
DCompilation error due to type mismatch
Attempts:
2 left
💡 Hint
The 'in' operator checks if a property exists on the object, helping TypeScript narrow the type.
🧠 Conceptual
intermediate
2:00remaining
Understanding 'in' operator narrowing with optional properties
Given the types below, which statement about the 'in' operator narrowing is true?
Typescript
type A = { x?: number };
type B = { y: string };

function test(obj: A | B) {
  if ('x' in obj) {
    // What can we say about obj here?
  }
}
AInside the if block, obj is narrowed to type A, but 'x' may still be undefined.
BInside the if block, obj is guaranteed to have property 'x' defined and not undefined.
CInside the if block, obj is narrowed to type B because 'x' is optional.
DThe 'in' operator cannot be used to narrow types with optional properties.
Attempts:
2 left
💡 Hint
The 'in' operator checks if the property exists, but optional properties may be undefined.
🔧 Debug
advanced
2:00remaining
Why does this 'in' operator narrowing fail?
Consider this TypeScript code snippet. Why does the compiler complain about accessing 'fly'?
Typescript
type Fish = { swim: () => void };
type Bird = { fly: () => void };

function move(animal: Fish | Bird) {
  if ('swim' in animal) {
    animal.fly();
  } else {
    animal.swim();
  }
}
ABecause 'fly' is not a property of Fish, the 'in' operator narrows animal to Bird, so animal.fly() is valid.
BThe compiler complains because 'fly' is not guaranteed to exist on animal even after the 'in' check.
CThe 'in' operator does not narrow union types when the property is not common to all types.
DThe compiler error occurs because 'swim' is missing in the else block.
Attempts:
2 left
💡 Hint
Check how TypeScript narrows types with the 'in' operator and property existence.
📝 Syntax
advanced
2:00remaining
Which option correctly uses 'in' operator for narrowing?
Which of the following TypeScript code snippets correctly narrows the type using the 'in' operator?
Aif ('swim' in animal) { animal.swim(); } else { animal.fly(); }
Bif (animal in 'swim') { animal.swim(); } else { animal.fly(); }
Cif ('swim' of animal) { animal.swim(); } else { animal.fly(); }
Dif ('swim' on animal) { animal.swim(); } else { animal.fly(); }
Attempts:
2 left
💡 Hint
Remember the correct syntax for the 'in' operator in TypeScript and JavaScript.
🚀 Application
expert
2:00remaining
Determine the number of keys in the narrowed object
Given the following TypeScript code, how many keys does the object 'obj' have inside the if block?
Typescript
type A = { a: number };
type B = { b: string };

function check(obj: A | B) {
  if ('a' in obj) {
    return Object.keys(obj).length;
  }
  return 0;
}

const result = check({ a: 1, extra: true } as A & { extra: boolean });
A0
B1
C3
D2
Attempts:
2 left
💡 Hint
Consider how Object.keys counts enumerable own properties regardless of type narrowing.