,angular,typescript,visual-studio-code,lodash"/>

首页 > 解决方案 > 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'.

不幸的是,我无法用值类型定义队列。删除此语法错误的选项有哪些?提前致谢。

标签: angulartypescriptvisual-studio-codelodash

解决方案


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价值。这会将 转换idany类型,匹配 的类型queue

let id: string = '122354';
let queue: any[] = records;
_.find(queue, {value: id as any})

推荐阅读