TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

When subclassing incorrectly, the compiler error suggests changing base class rather than the derived one

Open parzhitsky opened this issue 10 months ago • 0 comments

🔎 Search Terms

override method compiler error confusing message Target signature too few

🕗 Version & Regression Information

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/CYUwxgNghgTiAEkoGdnwGIHtPwN4Ch4j4BbEAFwAtNgAKASgC54A3TAS2HwF999QkcRNFTwAQrHggAHuRAA7YGiw4CxeJhYgYMTgjJUatWAHNm8gK4kARtqasOXXkA

💻 Code

declare class Foo {
    method(): void
}

declare class Bar extends Foo {
    override method(arg: number): void
    //       ~~~~~~
    // Error! Target signature provides too few arguments. Expected 1 or more, but got 0.
}

🙁 Actual behavior

An error message explains how base class has less arguments than expected

🙂 Expected behavior

An error message explains how derived class has more arguments than expected

Additional information about the issue

I understand that the compiler can't know whether the base class or the derived class is defined incorrectly. But:

  • normally, deriving from a class means that the developer wants to encapsulate the scope of work within this class and avoid changing the base class; also
  • seeing an error related to base class on a method of the derived class is unexpected and confusing.

parzhitsky avatar Mar 12 '25 10:03 parzhitsky