TS2394
error TS2394: This overload signature is not compatible with its implementation signature.
Broken Code ❌
The implementation does not match all signatures:
1 2 3 4 5
| function sum(a: number, b: number): number; function sum(a: string, b: string): string; function sum(a: number | string, b: number | string) { return `${parseInt(a + '', 10) + parseInt(b + '', 10)}`; }
|
Fixed Code ✔️
To match the first function overload, we have to add code to our function body which can also return a number
:
1 2 3 4 5 6 7 8
| function sum(a: number, b: number): number; function sum(a: string, b: string): string; function sum(a: number | string, b: number | string) { if (typeof a === 'number' && typeof b === 'number') { return a + b; } return `${parseInt(a + '', 10) + parseInt(b + '', 10)}`; }
|
Video Tutorial