admin管理员组文章数量:1323744
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 NodeJS.Timer
, which can't be used for clearTimeout
since clearTimeout requires a number.
How to fix that?
After some research I found the following type-cast:
let timer: null | ReturnType<typeof setTimeout> = null;
timer = setTimeout( myFunc, 1000 );
However, calling
clearTimeout(timer)
via TypeScript gives the following error:
TS2769: No overload matches this call.
Overload 1 of 2, '(timeoutId: Timeout): void', gave the following error.
Argument of type 'Timeout | null' is not assignable to parameter of type 'Timeout'.
Type 'null' is not assignable to type 'Timeout'.
Overload 2 of 2, '(handle?: number | undefined): void', gave the following error.
Argument of type 'Timeout | null' is not assignable to parameter of type 'number | undefined'.
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 NodeJS.Timer
, which can't be used for clearTimeout
since clearTimeout requires a number.
How to fix that?
After some research I found the following type-cast:
let timer: null | ReturnType<typeof setTimeout> = null;
timer = setTimeout( myFunc, 1000 );
However, calling
clearTimeout(timer)
via TypeScript gives the following error:
TS2769: No overload matches this call.
Overload 1 of 2, '(timeoutId: Timeout): void', gave the following error.
Argument of type 'Timeout | null' is not assignable to parameter of type 'Timeout'.
Type 'null' is not assignable to type 'Timeout'.
Overload 2 of 2, '(handle?: number | undefined): void', gave the following error.
Argument of type 'Timeout | null' is not assignable to parameter of type 'number | undefined'.
Share
Improve this question
asked Apr 18, 2021 at 22:00
deletedelete
19.2k17 gold badges62 silver badges98 bronze badges
4
-
1
But in TypeScript, the setTimeout-Function has a strange return value NodeJS.Timer, which can't be used for clearTimeout since clearTimeout requires a number.
clearTimeout accepts the value returned bysetTimeout
. – tkausl Commented Apr 18, 2021 at 22:05 - 4 You specified that timer can be null, but overload 1 (taking Timeout) does not accept null. Either assign in one statement, or use a null guard (if statement) – Lesiak Commented Apr 18, 2021 at 22:19
-
1
"But in TypeScript, the setTimeout-Function has a strange return value
NodeJS.Timer
" - no. It has that return type in Node.js, and it seems you have configured your Typescript to assume a node environment. And in node,clearTimeout
also accepts such a timer object. – Bergi Commented Apr 18, 2021 at 23:19 -
@delete what's the contents of your
tsconfig.json
? – Lambda Fairy Commented Apr 19, 2021 at 0:59
3 Answers
Reset to default 4If you are still looking for answer, you need to mention that you are accessing timeouts from window object instead of node
const { setTimeout, clearTimeout } = window
const timerId = setTimeout(() => {
... timeout code here
}, timeoutInMilliSeconds)
// Clear out the timer later
clearTimeout(timerID)
TS playground with window object
Add guard against null:
let timer: null | ReturnType<typeof setTimeout> = null;
timer = setTimeout( myFunc, 1000 );
if (timer) {
clearTimeout(timer);
}
I got the similar problem and managed to fix it with non-null assertion operator to tell the piler that "the expression cannot be null or undefined"
let timer: null | ReturnType<typeof setTimeout> = null;
timer = setTimeout(myFunc, 1000);
clearTimeout(timer!);
本文标签:
版权声明:本文标题:javascript - TS2769: How to fix "clearTimeout" with return value from setTimeout in TypeScript? - Stack Overfl 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742117561a2421539.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论