admin 管理员组文章数量: 1086019
I have a util that takes an array and a predicate to perform a filtration of the array, however after using my custom type for the predicate I am getting an error stating
property 'name' does not exist on type 'T'
I thought that the generic property type T
would have accepted anything?
Am I missing something obvious?
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];
}
Usage in test
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));
...
});
Of course, changing
I have a util that takes an array and a predicate to perform a filtration of the array, however after using my custom type for the predicate I am getting an error stating
property 'name' does not exist on type 'T'
I thought that the generic property type T
would have accepted anything?
Am I missing something obvious?
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];
}
Usage in test
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));
...
});
Of course, changing
Share Improve this question edited Oct 5, 2019 at 14:37 StepUp 38.3k16 gold badges92 silver badges157 bronze badges asked Oct 18, 2018 at 9:57 physicsboyphysicsboy 6,37822 gold badges77 silver badges139 bronze badges2 Answers
Reset to default 1Change your arrayPredicate declaration as shown below.
export type arrayPredicate<T> = (arg: T) => boolean;
class ArrayUtils {
static filterArray<T>(array: T[], arrayPred: arrayPredicate<T>): T {
const key = Object.keys(array).find(obj => arrayPred(array[obj]));
return array[key];
}
}
Since you are not declaring parameter in type for arrayPredicate, it is assumed to be empty object.
Here is working example,
TypeScript example
Need to add a type for your array for exmaple lets sat type of the array is SampleType[]
. Then is should be
const array: SampleType[] = [
{
'name': 'object-1',
'id': 1
},
{
'name': 'object-2',
'id': 2
}
];
Then pass that type to the generic function
ArrayUtils.filterArray<SampleType>(exceptionalStatuses, (status: SampleType => status.name === findStatus));
SampleType should be
export class SampleType{
name: string,
id: string
}
本文标签: javascriptProperty does not exist on type 39T39Generic problemsStack Overflow
版权声明:本文标题:javascript - Property does not exist on type 'T' - Generic problems - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744074216a2529045.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论