Challenge - 5 Problems
In Operator Narrowing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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));Attempts:
2 left
💡 Hint
The 'in' operator checks if a property exists on the object, helping TypeScript narrow the type.
✗ Incorrect
The 'in' operator narrows the union type Fish | Bird to Fish when 'swim' is present, so animal.swim() runs. Otherwise, it calls animal.fly(). The output is 'Swimming' then 'Flying'.
🧠 Conceptual
intermediate2: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?
}
}Attempts:
2 left
💡 Hint
The 'in' operator checks if the property exists, but optional properties may be undefined.
✗ Incorrect
The 'in' operator narrows obj to type A because only A has property 'x'. However, since 'x' is optional, it may still be undefined inside the block.
🔧 Debug
advanced2: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();
}
}Attempts:
2 left
💡 Hint
Check how TypeScript narrows types with the 'in' operator and property existence.
✗ Incorrect
The 'in' operator narrows animal to Fish in the if block, so animal.fly() is invalid. The compiler complains because after narrowing to Fish, 'fly' does not exist.
📝 Syntax
advanced2:00remaining
Which option correctly uses 'in' operator for narrowing?
Which of the following TypeScript code snippets correctly narrows the type using the 'in' operator?
Attempts:
2 left
💡 Hint
Remember the correct syntax for the 'in' operator in TypeScript and JavaScript.
✗ Incorrect
The correct syntax is 'propertyName' in object. Only option A uses this syntax correctly.
🚀 Application
expert2: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 });Attempts:
2 left
💡 Hint
Consider how Object.keys counts enumerable own properties regardless of type narrowing.
✗ Incorrect
The object passed has keys 'a' and 'extra', so Object.keys returns 2 keys inside the if block.