Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
katyapavlenko opened this issue Oct 28, 2019 · 5 comments
Open

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

katyapavlenko opened this issue Oct 28, 2019 · 5 comments

Comments

@katyapavlenko
Copy link

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
@DamonOehlman
Copy link
Owner

@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).

@katyapavlenko
Copy link
Author

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

@DamonOehlman
Copy link
Owner

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.

@mausworks
Copy link

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

Assuming global context is a bad idea anyways. :)

@katyapavlenko
Copy link
Author

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 :)

vilicvane added a commit to vilicvane/detect-browser that referenced this issue Nov 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants