In TypeScript, what does a
typeof x === "string" check inside an if block do to the type of x for the rest of that block?- aNothing — TypeScript never changes a variable's type based on runtime checks
- bNarrows
x's type tostringfor the rest of that block✓ - cConverts
xto astringat runtime, changing its actual value - dRemoves the type annotation from
xentirely
Explanation:This is TypeScript's core narrowing mechanism: a
typeof check is one of several recognized type guards. Inside the branch where the check is true, the compiler statically narrows the variable's type to string for all code within that block, so string-only methods become available without a cast.