[英]Argument of type 'number' is not assignable to parameter of type '{ [x: number] }'
Can someone help me solve this problem?有人可以帮我解决这个问题吗? Basically, I get an error saying
Argument of type 'number' is not assignable to parameter of type '{ [x: number] }'
基本上,我收到一条错误消息,说
Argument of type 'number' is not assignable to parameter of type '{ [x: number] }'
I am not sure how else would I describe my interface, but here's how it looks.我不知道我还能如何描述我的界面,但这是它的外观。
export interface Payload {
filter: Array<FilterArray>,
}
interface FilterArray {
[id: number]: Filter
}
interface Filter {
operator: string,
id: number,
type: string,
}
And when I try something like:当我尝试类似的事情时:
payloadFilter.filter.splice(payloadFilter.filter.indexOf(filterId), 1)
I get the error that I described above.我收到我上面描述的错误。 Any thoughts or someone who can help solve this?
任何想法或可以帮助解决此问题的人?
I can explain the error.我可以解释错误。 It's harder to explain how to fix it because I don't know what you are trying to do.
更难解释如何修复它,因为我不知道您要做什么。
I have a feeling that you aren't fully understanding your data types here.我有一种感觉,您没有完全理解此处的数据类型。 Assuming that
payloadFilter
is of type Payload
, that would be something like this:假设
payloadFilter
是Payload
类型,那将是这样的:
const someFilter: Filter = {/**...*/}
const payloadFilter: Payload = {
filter: [
{
0: someFilter,
},
{
7: someFilter,
99: someFilter,
}
]
}
Your typings say that the property filter
of Payload
is an array
of object
s with numeric keys and Filter
values.您的类型说
Payload
的属性filter
是一个带有数字键和Filter
值的object
array
。 Is that really what it looks like?真的是这个样子吗?
The function Array.prototype.indexOf
looks for the index of a specific value in an array, so the argument must match the type of that array's elements.函数
Array.prototype.indexOf
查找数组中特定值的索引,因此参数必须与该数组元素的类型匹配。 Here, the type of payloadFilter.filter
is Array<FilterElement>
and its elements have the type FilterElement
, so you can only call indexOf
with a FilterElement
.在这里,
payloadFilter.filter
的类型是Array<FilterElement>
并且它的元素的类型是FilterElement
,所以你只能用FilterElement
调用indexOf
。 Instead you are calling it with a number
and you get the error that you've posted here.相反,您使用一个
number
调用它,并且会收到您在此处发布的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.