Open
Description
π Search Terms
inferred type predicate optional
π Version & Regression Information
- This changed between versions 5.4 and 5.5
β― Playground Link
π» Code
type Obj = {optional?: {a: number}}
function isOptionalDefined(e: Obj) {
return e.optional !== undefined
}
if (isOptionalDefined(someObj) {
someObj.optional.a // 'someObj.optional' is possibly 'undefined'
}
π Actual behavior
If a type contains an optional property, and I write a function that ensures the property is defined, the function is not inferred as a type predicate. No type narrowing happens when I use this function.
At the same time, if I rewrite the original type to be itself a union, then the type predicate is inferred, as expected.
π Expected behavior
Type predicate is inferred
Additional information about the issue
I feel it has something to do with #55257, but I'm not sure.