Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tsc optional chaining with functions that may return void or an object #35818

Closed
dannypike opened this issue Dec 21, 2019 · 1 comment
Closed

Comments

@dannypike
Copy link

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?

@MartinJohns
Copy link
Contributor

Looks like a duplicate of #35236.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants