Mapped & Conditional Types

Preview — 3 of 10 questions

What type does TypeScript infer for direction?

javascript
const direction = "north";
A"north" — the literal type
Bstring
Cstring | undefined
Dnever

What does TypeScript know about value inside each branch?

javascript
function process(value: string | number) {
  if (typeof value === 'string') {
    // value is: ?
  } else {
    // value is: ?
  }
}
ABoth branches: string | number
BBoth branches: unknown
Cif branch: string | number; else branch: never
Dif branch: string; else branch: number

What does TypeScript narrow error to in the catch block?

javascript
try {
  riskyOperation();
} catch (error) {
  if (error instanceof Error) {
    console.log(error.message); // ?
  }
}
Aerror is unknown, so .message always errors even with instanceof
Berror in catch is typed as Error by default
CInside the if, TypeScript narrows error to Error — .message is safely accessible
Dinstanceof only works with custom classes, not built-in Error

Sign up free to play

Answer all 10 questions (7 more), see explanations for every answer, and track your score.