TypeScript - what type is f.e. setInterval

gfels picture gfels · Jul 17, 2018 · Viewed 36.6k times · Source

If I'd like to assign a type to a variable that will later be assigned a setInterval like so:

this.autoSaveInterval = setInterval(function(){
      if(this.car.id){
        this.save();
      }
      else{
        this.create();
      }
    }.bind(this), 50000);

What type should be assigned to this.autosaveInterval vairable?

Answer

Stav Bodik picture Stav Bodik · Mar 14, 2019

The type depends on which function you are going to use there are 2 overloads, the return type is marked in red bounding-box :

enter image description here

In order to use the one which returns number, please use :

window.setInterval(...)