The main issue at hand is emphasized in a GitHub post regarding TypeScript, specifically microsoft/TypeScript#9998.
Control flow analysis is implemented by the compiler to track variable types at different points in the code. This feature enables the compiler to detect errors like using a variable before assigning it:
let oops: string;
oops.toUpperCase(); // error, used before assigned
let okay: string;
okay = "assigned";
okay.toUpperCase(); // okay
While control flow analysis is helpful, it's not flawless. The compiler does not predict every possible execution path of a program and lacks human reasoning capabilities. Instead, it relies on heuristics, which are simplifying assumptions, for type checking during compilation.
The limitations become evident when control flow extends across function boundaries. Following the flow entirely into and out of a function is impractical for the compiler, so it makes assumptions that may lead to false positives (errors flagged for benign code) and false negatives (undetected bugs). This issue is discussed in microsoft/TypeScript#9998.
During the implementation of this control-flow analysis functionality, the compiler frequently raised warnings about uninitialized variables in the outer scope, potentially causing the specific error you encountered.
Regrettably, these warnings also appeared in scenarios where the outer variable was indeed initialized but went unnoticed by the compiler. This resulted in too many false alarms, leading to the submission of bug report microsoft/TypeScript#9757.
A minor adjustment to the assumption was made to address this issue, as detailed in microsoft/Typescript#10815. The compiler now "assumes outer variables are always initialized in control flow analysis."
This change eliminates certain false positives but introduces new cases of false negatives, like the one in your code example.
If you believe this behavior should be enhanced, you could consider submitting a new issue or suggestion through GitHub. It might be proposed that if a variable remains unassigned throughout the entire codebase, the compiler reverts to its pre-microsoft/TypeScript#10815 behavior: "assume an outer variable is assigned only if there is at least one assignment somewhere in the program."
However, the adoption of such suggestions is uncertain. Any additional analysis conducted during variable initialization checks incurs performance costs. Unless the added effort resolves real-world TypeScript issues, it may not be prioritized, even if it enhances the accuracy and comprehensiveness of the type checker.
The key question then becomes: how prevalent is this issue? While no definitive answer exists, instances seem relatively rare. I have yet to come across anyone reporting a situation exactly like yours.
In your case, the outer variable remains unassigned. Reports do exist where outer variables are utilized before assignment without detection by the compiler. Even in more common scenarios, little action seems to be taken on these matters, as evidenced in microsoft/TypeScript#42036.