You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
when narrowing a property in an object, there are often other things it should be able to narrow as well. for example, when narrowing foo?.bar to a non-undefined type, it should also narrow foo to not be undefined
π Motivating Example
declareconstfoo: {bar: number[]}|undefinedif(Array.isArray(foo?.bar)){foo.bar//error: Object is possibly 'undefined'}
π» Use Cases
i'm trying to conditionally modify an array, but only if its parent property is present. currently, i have to use ! to tell the compiler that it's not undefined, which isn't ideal:
Suggestion
π Search Terms
narrowing optional chaining
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
when narrowing a property in an object, there are often other things it should be able to narrow as well. for example, when narrowing
foo?.bar
to a non-undefined
type, it should also narrowfoo
to not beundefined
π Motivating Example
π» Use Cases
i'm trying to conditionally modify an array, but only if its parent property is present. currently, i have to use
!
to tell the compiler that it's not undefined, which isn't ideal:The text was updated successfully, but these errors were encountered: