Angular 带有';任何';输入寄存器';没有最佳通用类型';反正是错误

Angular 带有';任何';输入寄存器';没有最佳通用类型';反正是错误,angular,typescript,Angular,Typescript,尽管我已将函数指定为“any”类型,但我仍不断得到无最佳公共类型错误。我还尝试了“any | string”、“string | any”等类型的组合。非常感谢您的帮助 export class TestClass { gotCode: any; constructor() { this.gotCode = function(){ var codes = Lodash.compact(Lodash.map(Parse.User.current().get("

尽管我已将函数指定为“any”类型,但我仍不断得到无最佳公共类型错误。我还尝试了“any | string”、“string | any”等类型的组合。非常感谢您的帮助

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";
        }
     }();
  }
}
我查看了现有页面:


我对Typescript不太熟悉,无法解释错误发生的原因(我不认为Typescript中的
any
是“root”类型,因此与C#或Java中的
object
一样,对所有其他类型都很常见,但我不确定。)您应该能够通过给函数一个显式返回类型来修复错误:

export class TestClass {
    gotCode: any;

    constructor() {
        this.gotCode = function():any { // <- Add the return type here
            /* Your code here... */
        }();
    }
}
导出类TestClass{
代码:任何;
构造函数(){

this.gotCode=function():any{//我对Typescript不太熟悉,无法解释错误发生的原因(我不认为Typescript中的
any
是“root”类型,因此与C#或Java中的
object
一样,对所有其他类型都是通用的,但我不确定。)通过为函数提供显式返回类型,您应该能够修复错误:

export class TestClass {
    gotCode: any;

    constructor() {
        this.gotCode = function():any { // <- Add the return type here
            /* Your code here... */
        }();
    }
}
导出类TestClass{
代码:任何;
构造函数(){

this.gotCode=function():any{/您只需在类上声明方法即可。此外,为了避免“无最佳常见类型错误”,请将函数的返回类型设置为any

  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';
      }
    }
  }

您只需在类上声明方法即可。此外,为了避免“无最佳公共类型”错误,请将函数的返回类型设置为“任意”

  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';
      }
    }
  }

您不能将“:any”放在这里,因为它会抛出一个错误:“this.gotCode=function():any”{//错误是什么?类似的代码为我编译,将返回类型设置为
any
是您在JayChase的回答中接受的解决方案。我不确定发生了什么。结果是正确的。我认为在Ionic/Angular 2重建系统中可能存在错误。您不能在此处输入“:any”,因为它抛出了错误:this.gotCode=function():any{//错误是什么?类似的代码为我编译,将返回类型设置为
any
是您在JayChase的回答中接受的解决方案。我不确定发生了什么。结果是正确的。我想Ionic/Angular 2重建系统中可能存在错误。