TS2345

error TS2345: Argument of type ‘number‘ is not assignable to parameter of type ‘TimerHandler‘.

Broken Code ❌

1
2
3
4
5
function add(a: number, b: number): number {
return a + b;
}

setTimeout(add(1000, 337), 5000);

Fixed Code ✔️

There is a mismatch in the expected arguments of a function. The setTimeout function expects the first argument to be a callback function and not the returned value (in this case a number) of a function call:

1
2
3
4
5
function add(a: number, b: number): number {
return a + b;
}

setTimeout(() => add(1000, 337), 5000);

Video Tutorial