TypeScript · Middle · Question 74 of 75

Does narrowing work when a discriminated union is destructured first?

const { kind, radius } = shape;
if (kind === 'circle') radius.toFixed();

Explanation

The destructured variables must be const (or unmodified parameters) for control-flow analysis to connect them.

Further reading

Problem with a question

TypeScript · Middle · question 74

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