2016-12-09 3 views
1

「any」型の関数を割り当てたにもかかわらず、No best common typeエラーが続きます。私はまた、 'any | string'、 'string | any'のような型の組み合わせを試しました。 。 。ヘルプは非常に高く評価されます。'any'型のTypescript関数は、 'No best common type'のエラーがありません。

export class TestClass { 
    gotCode: any; 

    constructor() { 

    this.gotCode = function(){ 
     var codes = Lodash.compact(Lodash.map(Parse.User.current().get("blah"), function(n){ 
      if(moment().isBefore(n.endDate) && moment().isAfter(n.startDate)){ 
       return n; 
      } 
     })); 

     if (codes.length > 0){ 
      return {endDate: Lodash.first(codes).endDate, codeId: Lodash.first(codes).objectId, consumerMatchingCardCash: Lodash.first(codes).consumerMatchingCardCash, codeName: Lodash.first(codes).codeName, consumerPercentIncreaseOnCashBack: Lodash.first(codes).consumerPercentIncreaseOnCashBack}; 
     } else{ 
      return "No Codes"; 
     } 
    }(); 
    } 
} 

私は既存のページを見ていた: Type inference with union types - No best common type exists

Angular2: No best common type exists among return expressions

No best common type exists among return expressions

答えて

2

クラスに対してメソッドを宣言するだけで済みます。また、ラウンドするには、 '最高の一般的な型のエラーは、任意の関数の戻り値の型を設定します。

export class TestClass { 
    constructor() { 
    } 

    gotCode(): any { 
     let codes = Lodash.compact(Lodash.map(Parse.User.current().get('blah'), function (n) { 
     if (moment().isBefore(n.endDate) && moment().isAfter(n.startDate)) { 
      return n; 
     } 
     })); 

     if (codes.length > 0) { 
     return { 
      endDate: Lodash.first(codes).endDate, 
      codeId: Lodash.first(codes).objectId, 
      consumerMatchingCardCash: Lodash.first(codes).consumerMatchingCardCash, 
      codeName: Lodash.first(codes).codeName, 
      consumerPercentIncreaseOnCashBack: Lodash.first(codes).consumerPercentIncreaseOnCashBack 
     }; 
     } else { 
     return 'No Codes'; 
     } 
    } 
    } 
2

私はエラーが(Iドンを発生している理由を言って活字体で十分慣れていませんよTypescriptのanyは "ルート"タイプなので、他のすべてのタイプに共通すると思います。objectはC#またはJavaですが、私はsuではありません関数を明示的な戻り値の型にすることでエラーを修正できるはずです:

export class TestClass { 
    gotCode: any; 

    constructor() { 
     this.gotCode = function():any { // <- Add the return type here 
      /* Your code here... */ 
     }(); 
    } 
} 
+0

「this.gotCode = function():any {// < - 戻り値の型をここに追加してください。その他の提案? – rashadb

+0

エラーは何ですか?同様のコードが私のためにコンパイルされ、戻り値の型を 'any'に設定することはJayChaseの答えで受け入れた解決策です。 – Mattia

+0

何が起こっていたのか分かりません。それは大丈夫だった。私は、Ionic/Angular 2の再構築システムのどこかにバグがあるかもしれないと思います。 – rashadb

関連する問題