return 个表达式中不存在最佳通用类型

No best common type exists among return expressions

当我使用Collection2 in angular2-meteor project, these kinds of codes from 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();
      }
    }
  }
}

由于每个 return 分支都需要一种日期类型,因此您必须 return 每个 if/else 分支的日期类型,或者您可以创建一个 return 是两种不同的类型。

在任何一种情况下,如果类型是日期,您可以 return 为第三个条件设置 null。这在打字稿中有效。

autoValue: function() : Date|Object  {
    if (this.isInsert) {
        return new Date();
    } else if (this.isUpsert) {
        return {$setOnInsert: new Date()};
    } else {
        this.unset();
        return null;
    }
}