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 This overload signature is not compatible with its implementation signature

function overloading

I wrote the 2 interfaces. Also I wrote 3 overloading functions. And I have implementation of functions. And here an error – This overload signature is not compatible with its implementation signature

interface MyPosition {
  x: number | undefined
  y: number | undefined
}

interface MyPositionWithDefault extends MyPosition {
  default: string
}

function position(): MyPosition
function position(a: number): MyPositionWithDefault // Here`s an 
//error
function position(a: number, b: number): MyPosition
function position(a?: number, b?: number) {
  if (!a && !b) {
    return {x: undefined, u: undefined}
  }

  if (a && !b) {
    return {x: a, y: undefined, default: a.toString}
  }

  return {x: a, y: b}
}

Playground

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 :

The compiler for some reason cannot infer the correct return type, simply adding the annotation fixes the problem:

interface MyPosition {
  x: number | undefined
  y: number | undefined
}

interface MyPositionWithDefault extends MyPosition {
  default: string
}

function position(): MyPosition
function position(a: number): MyPositionWithDefault // No error
function position(a: number, b: number): MyPosition
function position(a?: number, b?: number): MyPosition | MyPositionWithDefault { // added return type annotation
  if (!a && !b) {
    return {x: undefined, y: undefined}
  }

  if (a && !b) {
    return {x: a, y: undefined, default: a.toString() }
  }

  return {x: a, y: b}
}

Playground

I also cleaned up some typos.

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