1

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.

2
  • 1
    You should make second and third arguments in the implementation signature optional with question mark syntax: withIdType?: ..., withEntityType?: .... TS doesn't treat optional argument in the same way as argument accepting undefined here for some reason Commented Feb 9, 2023 at 15:25
  • Thank you, that did the trick! If you provide an answer, I'll gladly accept it! Commented Feb 9, 2023 at 15:27

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Typescript never treats optional arguments and arguments that accept undefined as the same. Optional arguments can be excluded altogether, but arguments that take undefined must expressly be passed undefined.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.