简体   繁体   中英

Typescript Error: setInterval - Type 'Timer' is not assignable to type 'number'

I have the following code:

let onSizeChangeSetInterval = setInterval(() => {...}, 30);

When I compile this code, I'm getting the following error:

ERROR in src/components/popover/popover.component.ts(98,17): error TS2322: Type 'Timer' is not assignable to type 'number'. src/modules/forms-ui/formly/types/daterange/picker.daterange.component.ts(186,9): error TS2322: Type 'Timer' is not assignable to type 'number'.

使用window.setInterval代替

By looking at error TS2322 it looks like a TypeScript error rather than JavaScript one. Basically you are trying to cast type number variable to a timer one.

could you do instead ?

let onSizeChangeSetInterval:NodeJS.Timer;
onSizeChangeSetInterval = setInterval(() => {...}, 30);

Taken from

http://evanshortiss.com/development/nodejs/typescript/2016/11/16/timers-in-typescript.html

Use ReturnType :

let onSizeChangeSetInterval: ReturnType<typeof setInterval> | undefined;
onSizeChangeSetInterval = setInterval(() => {...}, 30);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM