加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

meteor – 返回表达式中不存在最常见的类型

发布时间:2020-12-17 07:28:21 所属栏目:安全 来源:网络整理
导读:当我在angular2-meteor项目中使用 Collection2时,demo的这些代码总是在终端给我警告: No best common type exists among return expressions. 我该如何改进代码?谢谢 { createdAt: { type: Date,autoValue: function() { if (this.isInsert) { return new
当我在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;
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读