9.7 Assignment Narrowing
Right, so you’ve got a variable that could be one of a few types. The big question is: how do you convince TypeScript’s ever-vigilant type checker that right now, at this specific line of code, it’s actually a specific one? One of the simplest and most common ways this happens is through the utterly mundane act of assignment. It’s so straightforward you might miss its power. When you assign a new value directly to a variable, TypeScript looks at that new value, checks its type, and says, “Well, alright then, I guess we’re doing this.” It promptly narrows the variable’s type from whatever it was before to the type of the new value. This is Assignment Narrowing.