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

Type discrimination in function calls for callable types doesn't work if discriminating property is optional #34529

Open
connorjclark opened this issue Oct 17, 2019 · 0 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@connorjclark
Copy link
Contributor

TypeScript Version: 3.7.0-dev.20191015

Search Terms: type discrimination, callable, signatures, implicit any

Code

function test(
  a:
    | { type?: 1; property: (a: number) => void }
    | { type: 2; property: (a: string) => void },
) {
  return a.property
}


test({
  property: (x) => { // complaining that x implicitly has type any
    console.log('hi')
  },
})

Expected behavior:

x should be inferred to be number.

Actual behavior:

x is any.

If you change type?: 1 to type: 1, it works as expected.

Playground Link: link

Related Issues: #7294 (closed with #29011 - see second half of the original post, I believe it describes why this issue occurs)

@connorjclark connorjclark changed the title Type discrimination in function calls for callable types don't work if discriminating property is optional Type discrimination in function calls for callable types doesn't work if discriminating property is optional Oct 17, 2019
@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Oct 17, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants