Visual Studio/Angular - 类型的参数不可分配给参数类型 ObjectIterateeCustom<any[],布尔值>



我正在使用打字稿 5.8.3 和 loadash 进行一个角度 3.8.3 项目。我使用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;

请注意对象迭代器的类型T。这意味着传递给迭代器的对象属性/值必须与作为object参数传递的类型匹配。

换句话说,_.find(*[], {value: *, otherProp: *})星号必须是相同的类型。

尝试

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

您还可以向该值添加as any。这会将id转换为与queue类型匹配的any类型。

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

queue的结构是什么?

_.find(queue, {value: id})

尝试将{value: id}替换为函数

const someFn = (el) => {
return el.id === id;
}

请不要使用任何类型,请尝试放置一个带有属性值的接口

let queue: Record[] = [];
let record:Record=_.chain(queue).find({value:id}).value();

只需显式声明类型参数T,而不是允许推断它:

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

相关内容

最新更新