detect-browser
detect-browser copied to clipboard
Type 'Timer' is not assignable to type 'number'
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 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).
I don't like hacks like this, so i just used browser-detect
instead of this lib :(
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.
The solution to this is to use window.setInterval
instead of setInterval
.
Assuming global context is a bad idea anyways. :)
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 :)