当我在angular2-meteor项目中使用
Collection2时,demo的这些代码总是在终端给我警告:
No best common type exists among return expressions.
我该如何改进代码?谢谢
{
createdAt: {
type: Date,autovalue: function() {
if (this.isInsert) {
return new Date();
} else if (this.isUpsert) {
return {$setonInsert: new Date()};
} else {
this.unset();
}
}
}
}
由于每个返回分支都需要一种Date类型,因此必须为每个if / else分支返回Date类型,或者您可以创建一个返回两种不同类型的union.
在任何一种情况下,如果类型为Date,则可以为第三个条件返回null.这在打字稿中是有效的.
autovalue: function() : Date|Object {
if (this.isInsert) {
return new Date();
} else if (this.isUpsert) {
return {$setonInsert: new Date()};
} else {
this.unset();
return null;
}
}