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 inference for method overrides in derived classes #16590

Closed
Dru89 opened this issue Jun 16, 2017 · 4 comments
Closed

Type inference for method overrides in derived classes #16590

Dru89 opened this issue Jun 16, 2017 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@Dru89
Copy link

Dru89 commented Jun 16, 2017

TypeScript Version: 2.4.0

Code

interface IPage {
    method: (ev: string | number) => any;
}

class Page implements IPage {
    method(e): any {
        // this "e" is implicitly any
    }
}

let x = new Page();
x.method = (e) => {
    // this "e" is inferred to be 'string | number'
}

Expected behavior:
The method override in Page should be inferred type string | number.

Actual behavior:
The method override in Page is implicitly the any type.

This seems like it should instead infer the parent type, because

  1. instance overrides already have this sort of inference (see the Contextual Type example about window.onmousedown), and
  2. if the class's override signature was something like method(e: object): any, there would be an error that says the class wasn't matching the interface's signature. (Class 'Page' incorrectly implements interface 'IPage'.)
@Dru89
Copy link
Author

Dru89 commented Jun 16, 2017

My apologies if this either isn't a bug (or feature request) or it's already been closed in a separate ticket.

I wasn't able to really find anything related to this in search, and it seemed sort of like a bug to me.

@DanielRosenwasser DanielRosenwasser changed the title Type Inference for Method Overrides Type inference for method overrides in derived classes Jun 16, 2017
@DanielRosenwasser
Copy link
Member

It's not easy to find - check out #1373. Unfortunately we weren't able to solve this, but I think we're still considering some ideas here.

@DanielRosenwasser DanielRosenwasser added the Duplicate An existing issue was already created label Jun 16, 2017
@Dru89
Copy link
Author

Dru89 commented Jun 17, 2017

Thanks for pointing that ticket out (and the rabbit hole of tickets that I found that came from it).

Should I subscribe to #10610 as a potential solution and to #10570 as the open ticket for this dupe? I can definitely see why this is hard to solve. 😄

@mhegazy
Copy link
Contributor

mhegazy commented Aug 17, 2017

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@mhegazy mhegazy closed this as completed Aug 17, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants