返回表达式之间不存在最佳通用类型 [英] No best common type exists among return expressions

查看:97
本文介绍了返回表达式之间不存在最佳通用类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在angular2-meteor项目中使用 Collection2 时,这些演示总是在终端给我警告:

When I use Collection2 in angular2-meteor project, these kinds of codes from demo always give me warning in the terminal:

返回表达式之间不存在最佳的通用类型.

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();
      }
    }
  }
}

推荐答案

由于每个BACK分支都需要Date类型,因此必须为每个if/else分支返回Date类型,或者可以创建一个返回两个的并集不同种类.

Since a type of Date is expected for EVERY return branch, you must return a Date type for every if/else branch OR you can create a union that returns two different types.

在任何一种情况下,如果类型为Date,则可以为第三个条件返回null.这在打字稿中有效.

In either case, you can return null for the third condition if the type is Date. That is valid in typescript.

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

这篇关于返回表达式之间不存在最佳通用类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆