Guys where is an error in this code,
I suppose, I am covering all possible situations,
compiler saying I am wrong here – function position(a: number): IPositionDefault ,
but if there is one argument I returning object that looks like IPositionDefault, no?
function position(): IPosition
function position(a: number): IPositionDefault
function position(a: number, b: number) :IPosition
function position(a?: number, b? :number){
if (!a && !b){
return {x: undefined, y: undefined}
}
if (a && !b){
return {x: a, y: undefined, default: a.toString()}
}
if (!a && b){
return {x: b, y: undefined, default: b.toString()}
}
return {x: a, y: b}
}
I expected to see no error in compiler
>Solution :
All you need for error to go away to do is to define a return type of your actual function implementation.
Change:
function position(a?: number, b? :number){
To:
function position(a?: number, b? :number): IPosition | IPositionDefault {