web-dev-qa-db-ja.com

TypeScript-どのタイプかsetInterval

変数に型を割り当てたい場合は、後でsetIntervalが割り当てられます。

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

This.autosaveInterval vairableにはどのタイプを割り当てる必要がありますか?

21
gfels

タイプは数値です。

private autoSaveInterval: number = setInterval( ()=>{console.log('123')},5000);
3
user3003238

Typeof演算子を使用して、次のような変数のデータ型を検索します。

typeofは、任意の型の単一のオペランドの前に置かれる単項演算子です。その値は、オペランドのタイプを指定する文字列です。

var variable1 = "Hello";
var autoSaveInterval;

this.autoSaveInterval = setInterval(function(){
      if(this.car.id){
        this.save();
      }
      else{
        this.create();
      }
    }.bind(this), 50000);
    
console.log("1st: " + typeof(variable1))
console.log("2nd: " + typeof(autoSaveInterval ))
1
Ash