This is something that just blocked me for a hot second in a build pipeline of ours - apparently when I outsource a boolean check on if a value is there or not into a variable it doesn't quite work?
Though only for properties apparently.
So this does cause TS errors:
function x(val: { current: undefined | number}): number {
const hasValue = val.current !== undefined
if(hasValue){
return val.current
}
return 5
}
While this does not:
function x(val: undefined | number): number {
const hasValue = val !== undefined
if(hasValue){
return val
}
return 5
}
I'm trying to look more into this but I'm not sure what this is called so what to google for.
Is this a bug?
Is this a missing feature that is super complex to implement?
Is this intentional and I don't see why/how?