angular - Visual Studio/Angular - 类型参数不可分配给参数类型 ObjectIterateeCustom
问题描述
我正在使用 typescript 3.8.3 和 loadash 进行 angular 5 项目。我使用 Visual Studio Code 作为我的编辑器。我最近将我的 Visual Studio 代码更新到了 1.24.0 版
更新后,我在 Visual Studio 代码中遇到了一些代码语法错误。这些错误不会导致任何编译器故障,而是在我的代码中显示为红色。我得到的一个烦人的问题是使用负载的以下代码:
let id: string = '122354';
let queue: any[] = records;
_.find(queue, {value: id}) // loads iteration function
我的错误信息
Argument of type '{ value: string; }' is not assignable to parameter of type 'ObjectIterateeCustom<any[], boolean>'.
Type '{ value: string; }' is not assignable to type 'ObjectIterator<any[], boolean>'.
Type '{ value: string; }' provides no match for the signature '(value: any, key: string, collection: any[]): boolean'.
不幸的是,我无法用值类型定义队列。删除此语法错误的选项有哪些?提前致谢。
解决方案
lodash 的find
方法有一个类型定义,如
find<T>(
object: _.Dictionary<T>,
iterator: _.ObjectIterator<T, boolean>,
context?: any): T | undefined;
注意 ObjectIterator 的类型T
。这意味着传递给迭代器的对象属性/值必须与作为object
参数传递的类型相匹配。
换句话说,_.find(*[], {value: *, otherProp: *})
星号必须是相同的类型。
尝试
let id: any= '122354';
let queue: any[] = records;
_.find(queue, {value: id})
您还可以增加as any
价值。这会将 转换id
为any
类型,匹配 的类型queue
。
let id: string = '122354';
let queue: any[] = records;
_.find(queue, {value: id as any})
推荐阅读
- javascript - 对于 Protractor e2e 测试... 执行方法不稳定:单击按钮 3 次
- c# - Refactoring to make code open for extensions but closed for modifications
- python - nginx and django setting for docker-compose
- laravel - Laravel 5.8控制器索引函数返回空白页
- regex - 如何用bash中变量的值替换第i行的第j列
- findbugs - How can i install findbugs in netbeans 11.3
- php - 如何使用 formData 更新 laravel 中的帖子?
- node.js - 如何为heroku部署的nodejs bot设置viberbot webhook?
- php - oci8.so 中的代码签名在 Macos Mojave 中启用 SIP 时无效
- c# - 我需要帮助来理解 Odoo 的 REST-API