This might seem like an odd question at first, but I'll try to explain it the best I can.
Let's take a look at this following (and non-realistic) example:
const foo = (x: number) => {
return x * x;
}
const isXUndefined = (x?: number) => x === undefined;
const boo = (x?: number) => {
if (isXUndefined(x)) {
return 0;
}
return foo(x);
}
As expected, this code fails compiling since x can be both number and undefined.
What I struggle to see here - is why invoking isXUndefined isn't enough for the type inferrer to assure that x is an actual number?
A working example on the other hand would be:
const foo = (x: number) => {
return x * x;
}
const isXUndefined = (x?: number) => x === undefined;
const boo = (x?: number) => {
if (x === undefined) {
return 0;
}
return foo(x);
}
Why is this validation only working when making it "on the surface" but not from within another function?
Here's a playground with both examples