当我使用收藏2 https://github.com/aldeed/meteor-collection2在 angular2-meteor 项目中,这些代码来自demo https://github.com/aldeed/meteor-collection2总是在终端给我警告:
返回表达式中不存在最佳通用类型。
我该如何改进代码?谢谢
{
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 类型,或者您可以创建一个返回两种不同类型的联合。
无论哪种情况,如果类型为 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;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)