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.

>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

Leave a Reply