【发布时间】:2019-03-23 02:44:03
【问题描述】:
我有一个使用数组和谓词来执行数组过滤的实用程序,但是在将我的自定义类型用于谓词之后,我收到一条错误消息
类型“T”上不存在属性“名称”
我认为通用属性类型T 会接受什么?
我是否遗漏了一些明显的东西?
Array.ts
export type arrayPredicate = <T>(arg: T) => boolean;
ArrayUtil
static filterArray<T>(array: T[], arrayPred: arrayPredicate): T {
const key = Object.keys(array).find(obj => arrayPred(array[obj]));
return array[key];
}
在测试中的使用
const array = [
{
'name': 'object-1',
'id': 1
},
{
'name': 'object-2',
'id': 2
}
];
it(... , () => {
// I get red squigglies under '.name'
const myObj = ArrayUtils.filterArray(exceptionalStatuses,
(status => status.name === findStatus));
...
});
当然,改变
【问题讨论】:
标签: javascript angular typescript types