0
0
Typescriptprogramming~20 mins

Optional chaining with types in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Optional Chaining 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 optional chaining with nested objects?
Consider the following TypeScript code using optional chaining. What will be logged to the console?
Typescript
type User = {
  name: string;
  address?: {
    city?: string;
  };
};

const user: User = { name: "Alice" };

console.log(user.address?.city ?? "Unknown city");
A"Unknown city"
B"undefined"
C"Alice"
DThrows a runtime error
Attempts:
2 left
💡 Hint
Optional chaining returns undefined if the property before it is undefined or null.
Predict Output
intermediate
2:00remaining
What is the value of variable 'result' after optional chaining?
Given this TypeScript code, what is the value of 'result'?
Typescript
interface Config {
  settings?: {
    theme?: string;
  };
}

const config: Config = { settings: { theme: "dark" } };

const result = config.settings?.theme ?? "light";
Aundefined
B"light"
C"dark"
Dnull
Attempts:
2 left
💡 Hint
Check if the optional chaining accesses a defined property.
Predict Output
advanced
2:00remaining
What error does this code raise when accessing a possibly undefined method?
Examine this TypeScript code snippet. What error will occur at runtime?
Typescript
type ApiResponse = {
  data?: {
    getValue?: () => number;
  };
};

const response: ApiResponse = {};

const value = response.data?.getValue?.();
console.log(value.toFixed(2));
ASyntaxError: Unexpected token '.'
BReferenceError: value is not defined
CNo error, logs 'undefined'
DTypeError: Cannot read properties of undefined (reading 'toFixed')
Attempts:
2 left
💡 Hint
Optional chaining returns undefined if the method does not exist, then calling a method on undefined causes an error.
🧠 Conceptual
advanced
2:00remaining
How does optional chaining affect type narrowing in TypeScript?
Which statement best describes how optional chaining influences type narrowing in TypeScript?
AOptional chaining does not narrow the type; the result type always includes undefined.
BOptional chaining narrows the type to exclude undefined or null for the accessed property.
COptional chaining converts all types to 'any' to avoid errors.
DOptional chaining forces the property to be non-nullable at compile time.
Attempts:
2 left
💡 Hint
Think about what happens to the type when you use ?. operator.
Predict Output
expert
3:00remaining
What is the output of this complex optional chaining with arrays and functions?
Analyze the following TypeScript code and determine what is printed to the console.
Typescript
type User = {
  getFriends?: () => Array<{ name: string; age?: number }>;
};

const user: User = {
  getFriends: () => [
    { name: "Bob", age: 25 },
    { name: "Carol" },
  ],
};

const friendAge = user.getFriends?.()[1]?.age ?? "Age unknown";
console.log(friendAge);
Aundefined
B"Age unknown"
CThrows a runtime error
D25
Attempts:
2 left
💡 Hint
Check if the second friend has an age property defined.