Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

TypeScript: "Overload signature is not compatible with function implementation" in class method overload

The TypeScript compiler complains with "This overload signature is not compatible with its implementation signature." for any of the following overloads:

export class FullId {
  // other stuff

  static parse(toParse: string): FullId;
  static parse(toParse: string, withIdType: 'U' | 'S' | 'O' | 'T'): FullId;
  static parse(
    toParse: string,
    withIdType: 'U' | 'S' | 'O' | 'T' | undefined,
    withEntityType: string | undefined
  ): FullId;

  static parse(
    toParse: string,
    withIdType: 'U' | 'S' | 'O' | 'T' | undefined,
    withEntityType: string | undefined
  ): FullId {
    // my implementation
   }
}

It’s basically a method which can be called with one, two, or three arguments.
I don’t really understand what the problem is: I’ve created overloads distinct from the implementation, and in the implementation any but the first argument is optional. Removing the static modifier doesn’t change anything as far as I can tell.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Make the arguments optional using the question mark syntax

export class FullId {
  static parse(toParse: string): FullId;
  static parse(toParse: string, withIdType: 'U' | 'S' | 'O' | 'T'): FullId;
  static parse(
    toParse: string,
    withIdType: 'U' | 'S' | 'O' | 'T' | undefined,
    withEntityType: string | undefined
  ): FullId;

  static parse(
    toParse: string,
    withIdType?: 'U' | 'S' | 'O' | 'T',
    withEntityType?: string
  ): FullId {
    // ...
  }
}

For some reason TS doesn’t treat optional argument and argument accepting undefined in the same way here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading