0

To give a little background i was initially facing this issue and as shared by the solution, it make sense to let the type to be flexible depending on the context

However, I found out that it isnt really working on my end

let timeoutId: null | ReturnType<typeof setTimeout> = null

timeoutId = setTimeout(...)

With the code above, i'm getting Type 'number' is not assignable to type 'Timeout'.ts(2322).

When I hover over timeoutId, I got let timeoutId: NodeJS.Timeout | null, while hovering over setTimeout, i see below

function setTimeout(handler: any, timeout?: any, ...args: any[]): number (+2 overloads)
namespace setTimeout

Is there any way to go about it? I think the root cause is due to function overriding

Isaac
  • 12,042
  • 16
  • 52
  • 116
  • 1
    Please provide a [mre] that clearly demonstrates the issue you are facing. Ideally someone could paste the code into a standalone IDE like [The TypeScript Playground (link here!)](https://tsplay.dev/WKqVGN) and immediately get to work solving the problem without first needing to re-create it. So there should be no pseudocode, typos, unrelated errors, or undeclared types or values. – jcalz Apr 07 '22 at 03:01
  • What do you mean by "overriding" here? [Overriding](https://www.typescriptlang.org/docs/handbook/2/classes.html#overriding-methods) is not the same thing as [overloading](https://www.typescriptlang.org/docs/handbook/2/functions.html#function-overloads); do you maybe mean "overloading" instead? – jcalz Apr 07 '22 at 03:03
  • Is this code in a browser or node environment? – kelsny Apr 07 '22 at 03:04
  • @jcalz: it is not reproducible on TS playground because the absence of `@types/node` in the node_modules, I've mentioned in the question that timeoutId will be inferred to `let timeoutId: NodeJS.Timeout|null` – Isaac Apr 07 '22 at 03:11
  • @kellys: It should be NodeJS, but when I hover over `setTimeout`, TS thinks that it is on browser, that's why it's showing `function setTimeout(...): number`? – Isaac Apr 07 '22 at 03:14
  • @Isaac yes, you might need to modify the example code to make it reproducible in a standalone IDE. If you do that, you'll be more likely to get a useful answer than if you rely on others to either do it themselves or make untested suggestions. – jcalz Apr 07 '22 at 03:20

0 Answers0