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
TypeScript Version: 3.8.0-dev.20191221
(it also behaves this way with 3.7.3 and 3.7.4)
Code
class A {
prop: number = 1
}
declare function elsewhere() : void | A
let a = elsewhere()
let b = a?.prop
Expected behavior:
I was expecting the optional chaining to handle the two possibilities:
(a) elsewhere() returns an object that has a property called prop; and
(b) elsewhere() returns undefined.
Actual behavior:
The compiler rejects the code:
index.ts:8:12 - error TS2339: Property 'prop' does not exist on type 'void | A'.
Property 'prop' does not exist on type 'void'.
let b = a?.prop
~~~~
Found 1 error.
Have I misunderstood the way that optional chaining is supposed to work?
The text was updated successfully, but these errors were encountered:
TypeScript Version: 3.8.0-dev.20191221
(it also behaves this way with 3.7.3 and 3.7.4)
Code
Expected behavior:
I was expecting the optional chaining to handle the two possibilities:
(a)
elsewhere()
returns an object that has a property calledprop
; and(b)
elsewhere()
returnsundefined
.Actual behavior:
The compiler rejects the code:
Have I misunderstood the way that optional chaining is supposed to work?
The text was updated successfully, but these errors were encountered: