-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
setTimeout - assigning timeout id to a variable throws an error TS2323: Type 'Timer' is not assignable to type 'number'. #842
Comments
Please post a complete example. The information given so far is not enough to reproduce the issue: class Foo {
private sTimeout: number;
showDelay() { }
bar() {
setTimeout(() => this.showDelay(), 250);
}
} There is no built-in type called |
Should have made an example - was a little rushed earlier. Here you go:
Throws the following error: Using tsc v1.1.0-1 For now I have fixed it using by typing sTimeout to any . |
This does not repro for me. What happens when you F12 |
Not sure what you mean by F12 ? . |
"Go to definition" Again, there is no built-in type called |
Tried the example by itself: looks like it is caused by a component in my toolchain or other code - sorry for taking your time. Thank you ... Closed... |
For future readers, I came across this error because of the node.js definitely-typed definitions. The solution is to use the full name of the type, which is |
@jdfreder time flies - the future is here anyway I had the same issue: The main problem is that one of our 3rd party npm package (angular2) have TS + typings (node, and others) included in the package. |
@rixrix (and for anyone else who finds this) I had a similar problem until I realized I wasn't passing in the correct parameters. Compare this, which requires the second parameter
...and this, which does makes the second parameter
Based on these two functions TS correctly selects an appropriate overload, so if you get |
Hi @Penryn thanks for the heads up! I think my previous comment is a testament of not giving enough details as I can't remember anymore the exact details about the said error message. sorry. On top of my head, the issue that we had before was when we do incremental upgrades of our spin off web app written in Angular2(alpha releases). Our main code base (ng1 and ng2) is written in TypeScript (with bower components, bunch of other typings, and few custom typings), and Angular2 at that time were shipping typings as well, node typings etc. With our node typings (outdated version) and ng2 node typings (and possibly other typings), effectively the TS compiler gets confused about dups typings. In our case, we could have manually modify the offending typings but it's just too much work knowing that some point we'll have to pull out the latest version(s) I think this has been resolved in recent Angular2 releases as we'll have to manually install the required typings. cheers |
@RyanCavanaugh have same issue. I look, that I have two deffinitions for setTimeout. One returns |
Just use |
Why does the |
@cchamberlain Node actually returns an entire Timer class: https://nodejs.org/api/timers.html So not just a |
@Penryn - TIL 👍 |
For those curious, the reason this is happening is that So the reason you're seeing this problem is almost certainly because you've intended to target the web, but you've pulled in node definitions. |
@johnfn could also be isomorphic code intended to target browser and node. |
For others coming in here using the |
To add on to @AskYous , if you have |
For who is using Eq: tsconfig.app.json {
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/app",
"module": "es6",
"baseUrl": "",
"types": ["node"] --> ADD THIS
},
"exclude": [
"test.ts",
"**/*.spec.ts"
]
} I found it here: https://stackoverflow.com/a/43952363/3415716 |
Node.js' setTimeout returns entire Timer object and after upgrade the TypeScript considered setTimeout returns NodeJS.Timer object instead of a number. Changing to use window.setTimeout to distinguish with Node.js. See more details: microsoft/TypeScript#842
Maybe you have import a package that has a function with name |
in node environment type is |
details are here: microsoft/TypeScript#842
today after installing the |
Like @nippur72 , I ran into this today with |
Using tsc v1.1.0-1 we get an error when compiling :
error TS2323: Type 'Timer' is not assignable to type 'number'.
the following code throws the compile error:
this.sTimeout = setTimeout( () => this.showDelay() , 250 );
sTimeout is defined as a number ( private var ) .
The text was updated successfully, but these errors were encountered: