Ts2769: How To Fix "cleartimeout" With Return Value From Settimeout In Typescript?
In 'normal' JavaScript, I can do the following: var timer = setTimeout( myFunc, 1000 ); clearTimout(timer); But in TypeScript, the setTimeout-Function has a strange return value N
Solution 1:
Add guard against null:
lettimer: null | ReturnType<typeofsetTimeout> = null;
timer = setTimeout( myFunc, 1000 );
if (timer) {
clearTimeout(timer);
}
Solution 2:
If you are still looking for answer, you need to mention that you are accessing timeouts from window object instead of node
const { setTimeout, clearTimeout } = windowconst timerId = setTimeout(() => {
... timeout code here
}, timeoutInMilliSeconds)
// Clear out the timer laterclearTimeout(timerID)
TS playground with window object
Solution 3:
I got the similar problem and managed to fix it with non-null assertion operator to tell the compiler that "the expression cannot be null or undefined"
lettimer: null | ReturnType<typeofsetTimeout> = null;
timer = setTimeout(myFunc, 1000);
clearTimeout(timer!);
Post a Comment for "Ts2769: How To Fix "cleartimeout" With Return Value From Settimeout In Typescript?"