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

TypeScript not enforcing the return type of optional methods defined in interfaces implemented by parents of classes #30954

Open
G-Rath opened this issue Apr 16, 2019 · 1 comment
Labels
Bug A bug in TypeScript
Milestone

Comments

@G-Rath
Copy link

G-Rath commented Apr 16, 2019

TypeScript Version: 3.5.0 (Also tried 3.4.1, 3.0.1, and 2.4.1)

Search Terms: Inheritance, interface, return type, parent, child, override, overload, abstract, optional method, optional interface, implements, extends

Code

interface Mx {
  aMethod?(): number;
}

abstract class Base implements Mx {

}

class Child extends Base {
  public aMethod() {
    return 'hello world';
  }
}

Expected behavior:
TypeScript tells me that the return type for Child#aMethod is wrong.

This behaviour is achieved if I add implements Mx on the Child class.

Actual behavior:
TypeScript tells me nothing! It feels that Child#aMethod is well within its rights to return a string.

Playground Link: linky!

Related Issues: I didn't really find any similar looking issues?

The result is this could maybe relate to #22815

@G-Rath G-Rath changed the title TypeScript not enforcing the return type of optional methods defined in interfaces implemented by parents of classes? TypeScript not enforcing the return type of optional methods defined in interfaces implemented by parents of classes Apr 16, 2019
@RyanCavanaugh RyanCavanaugh added Working as Intended The behavior described is the intended behavior; this is not a bug Bug A bug in TypeScript and removed Working as Intended The behavior described is the intended behavior; this is not a bug labels Apr 16, 2019
@RyanCavanaugh
Copy link
Member

This is the intended behavior, though your code should have an error. implements does not cause optional methods to exist, so your base class is empty, so it's fine for your derived class to add a new method. Note that if you had any properties in your base class, you'd see an error about "implementing" the interface without actually doing anything.

The bug here is that we should have complained about the implements clause even though the class is empty

@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Apr 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants