Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No best common type exists among return expressions

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.

How can I improve the codes? Thanks

{
  createdAt: {
    type: Date,
    autoValue: function() {
      if (this.isInsert) {
        return new Date();
      } else if (this.isUpsert) {
        return {$setOnInsert: new Date()};
      } else {
        this.unset();
      }
    }
  }
}
like image 534
Hongbo Miao Avatar asked Mar 26 '16 03:03

Hongbo Miao


1 Answers

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.

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;
    }
}
like image 59
David L Avatar answered Oct 09 '22 05:10

David L