microsoft / microsoft/TypeScript

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

Open
#30,954 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: classes
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the example in the linked TypeScript Playground, then compare it with the version where Child explicitly implements Mx. The work is done when TypeScript reports the incompatible string return type for Child#aMethod without requiring the redundant implements clause.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.