【问题标题】:typescript: problems with angular.bind and map打字稿:angular.bind 和 map 的问题
【发布时间】:2015-08-30 18:20:23
【问题描述】:

我有以下代码:

  function parseValueFromComplexType(complexType, item) {
                return item[complexType];
            }

为了绑定复杂类型的值,我使用了 angular.bind

let parseValueFromComplexTypeWithValue = angular.bind('', parseValueFromComplexType , config.complexType);
val.values = val.values.map(parseValueFromComplexTypeWithValue);

现在打字稿抱怨:

错误 TS2345:“函数”类型的参数不可分配给“(值:任意,索引:数字,数组:任意 [])=> {}”类型的参数。

错误是什么意思,我该如何摆脱它?

【问题讨论】:

    标签: angularjs typescript


    【解决方案1】:

    map 函数接受具有三个参数的函数,结构如下:

    callbackfn: (value: T, index: number, array: T[]) => U
    

    angular的bind函数返回一个简单的Function类型:

    bind(context: any, fn: Function, ...args: any[]): Function;
    

    错误提示:您不能只将Function 放在应有callbackfn: (value: T, index: number, array: T[]) => U 的位置。

    这里的另一个问题是你试图以一种非常奇怪的方式使用角度 bind...如果你想保留 this 只需使用箭头函数。

    也许像这样(更简单):

    var complexType = 'str';
    val.values = val.values.map((item, index, array) => item[complexType]);
    

    【讨论】:

      猜你喜欢
      • 2021-08-02
      • 2020-02-29
      • 1970-01-01
      • 2017-09-26
      • 2021-03-17
      • 2021-06-19
      • 2020-09-25
      • 2019-02-01
      • 1970-01-01
      相关资源
      最近更新 更多