javascriptangulartypescripttypescript2.0

TypeScript - what type is setInterval


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?


Solution

  • Late to the party, but the best type (especially since the type is opaque, we only care that we can pass it to clearInterval() later) might be the automatically deduced one, ie. something like:

    ReturnType<typeof setInterval>