TypeScript · Middle · Question 75 of 75

What does switch (true) narrowing (TS 5.3) enable?

switch (true) {
  case typeof x === 'string': return x.toUpperCase();
  case Array.isArray(x): return x.length;
}

Explanation

It's a readable alternative to long if/else if chains with type guards.

Further reading

Problem with a question

TypeScript · Middle · question 75

Test yourself

Take the TypeScript · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz