I've been playing with the code in question Unexpected "never" in Typescript
I am trying to understand the undefined guard, and assumed that the variable is inferred to be not null / undefined in the scope of entire block following the check.
interface IProperties {
foo?: {
aaa: string
bbb: string
}
}
function init(properties: IProperties) {
if (properties.foo) {
type FooOK = typeof properties.foo;
for (const x of [1, 2, 3]) {
type FooOrUndefined = typeof properties.foo;
}
}
}
The types are inferred as follows:
type FooOK = { aaa: string; bbb: string;}
type FooOrUndefined = { aaa: string; bbb: string;} | undefined
Could anyone explain why the type in the loop does not take the guard into account?