【问题标题】:Errors after lodash upgradelodash 升级后的错误
【发布时间】:2019-12-16 11:51:58
【问题描述】:

我为一个 Angular JS 项目升级了 lodash 库 (v 2.4.1 到 4.17.15) 和 types/lodash(v 3.10.1 到 4.14.136)。升级后 _.find 出现错误。 错误说明

: '{ 'Id': any; 类型的参数}' 不可分配给“ListIteratorTypeGuard”类型的参数。

this.search = _.find<{}, IdModel>(this.Idlist, {'Id': Id});

我们如何将它转换为 ListIteratorTypeGuard? 我不会找到任何解决方案。如果有人知道如何解决它,请提供帮助。

【问题讨论】:

    标签: angularjs typescript types lodash restangular


    【解决方案1】:

    如果您想强制对速记对象进行部分检查,这应该会有所帮助。

    type IdModel = { id: number, foo: string }; const list: Array<IdModel> = [{ id: 1, foo: 'test'}]; find<IdModel>(list, { id: 1});

    但大多数情况下,您不应该指定通用参数,而是使用默认推断参数

    【讨论】:

    • 如何将参数转换为 ListIteratorTypeGuard?我的代码有什么错误?
    • 您好,您的代码没有错误,通过显式设置泛型参数,就像您所做的那样,您选择了一种查找函数类型(假设其中有 10-15 个),它期望迭代器函数作为第二个参数。只需使用 find 就可以了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多