Advanced TypeScript
Q100 / 100

What is "type narrowing via assignment" and how does control flow analysis use it, e.g. "let x: string | number = getValue(); x = 5;"?

Correct! Well done.

Incorrect.

The correct answer is B) After the assignment "x = 5", TypeScript narrows the type of x to "number" for subsequent code in that flow, based on the assigned value's type

B

Correct Answer

After the assignment "x = 5", TypeScript narrows the type of x to "number" for subsequent code in that flow, based on the assigned value's type

Explanation

TypeScript's control flow analysis tracks the type of a variable through assignments, narrowing it to the type of the most recently assigned value within the reachable code path.

Progress
100/100