detect-browser icon indicating copy to clipboard operation
detect-browser copied to clipboard

Type 'Timer' is not assignable to type 'number'

Open katyapavlenko opened this issue 5 years ago • 5 comments

After adding this package to my angular project, I got compilation errors. If i remove the module import, everything operates normally

import { detect } from 'detect-browser'
let timer: number;
timer = setInterval(() => {console.log('test')}, 1000)

leads to error TS2322: Type 'Timer' is not assignable to type 'number'. If i remove import – everything operates normally

versions:

detect-browser: 4.8.0
typescript: 2.5.3

katyapavlenko avatar Oct 28 '19 15:10 katyapavlenko

@katyapavlenko I'm familiar with this error as I've seen it in TS projects that use setInterval / setTimer. The issue is usually due to the fact that both of these functions are defined as having one return type for the DOM definitions (lib.dom.d.ts) and the node types (@types/node).

The only reason I can think that the inclusion of detect-browser is triggering this condition is that detect-browser does require the lib definitions to function correctly, and has them listed in it's tsconfig.json file.

I just did a quick Internet search and it does seem that you are not alone in having this error, and this stack overflow post might be worth checking to see if it fixes your problem.

(in short, they suggest to try using window.setInterval instead).

DamonOehlman avatar Oct 28 '19 22:10 DamonOehlman

I don't like hacks like this, so i just used browser-detect instead of this lib :(

katyapavlenko avatar Oct 29 '19 09:10 katyapavlenko

Cool, no problem at all - glad you found a solution.

The fact that browser-detect works and this package does not makes me wonder if it's something that's influenced by the TypeScript version. You are using 2.x and the browser-detect package is also on a 2.x build, whereas this repo is built against 3.x. If I get some time at some stage I'll do an investigation around that.

Thanks again for reporting the issue.

DamonOehlman avatar Oct 29 '19 22:10 DamonOehlman

The solution to this is to use window.setInterval instead of setInterval.

Assuming global context is a bad idea anyways. :)

mausworks avatar Nov 06 '19 10:11 mausworks

You are right, but in terms of developing an opensource package it's better to be ready for any code and not only for proper one :)

katyapavlenko avatar Nov 06 '19 10:11 katyapavlenko