-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Open
Labels
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScriptAn idea for TypeScript
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.
yukimotochern and andy-schneidr
Metadata
Metadata
Assignees
Labels
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScriptAn idea for TypeScript