Skip to content

Type inference for method overrides in derived classes #16590

Closed
@Dru89

Description

@Dru89

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'.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions