TypeScript - fe setInterval 是什么类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51376589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeScript - what type is f.e. setInterval
提问by gfels
If I'd like to assign a type to a variable that will later be assigned a setInterval like so:
如果我想为一个变量分配一个类型,该变量稍后将被分配一个 setInterval ,如下所示:
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?
应该为 this.autosaveInterval 变量分配什么类型?
采纳答案by user3003238
The type is number;
类型是数字;
private autoSaveInterval: number = setInterval( ()=>{console.log('123')},5000);
回答by Stav Bodik
回答by Joachim Berdal Haga
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:
迟到了,但最好的类型(特别是因为类型是不透明的,我们只关心我们可以将它传递给clearInterval()
以后)可能是自动推导的,即。就像是:
ReturnType<typeof setInterval>
回答by Ash
Use typeof operator to find data type of any variable like this:
使用 typeof 运算符查找任何变量的数据类型,如下所示:
typeof is an unary operator that is placed before a single operand which can be of any type. Its value is a string that specifies the type of operand.
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 ))