简体   繁体   English

'number' 类型的参数不能分配给类型 '{ [x: number] }' 的参数

[英]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:假设payloadFilterPayload类型,那将是这样的:

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.

相关问题 React Typescript:'{ [x: number]: any; 类型的参数 }' 不可分配给类型的参数 - React Typescript: Argument of type '{ [x: number]: any; }' is not assignable to parameter of type “数字”类型的参数不能分配给“字符串”类型的参数 - Argument of type 'number' is not assignable to parameter of type 'string' “数字”类型的参数不能分配给“日期”类型的参数 - Argument of type 'number' is not assignable to parameter of type 'Date' “字符串”类型的参数不能分配给“数字”类型的参数 - Argument of type 'string' is not assignable to parameter of type 'number' '(number | null)[]' 类型的参数不能分配给 'number[]' 类型的参数 - Argument of type '(number | null)[]' is not assignable to parameter of type 'number[]' &#39;string | 类型的参数 number&#39; 不能分配给类型为 &#39;string&#39; 的参数。 “数字”类型不能分配给“字符串”类型 - Argument of type 'string | number' is not assignable to parameter of type 'string'. Type 'number' is not assignable to type 'string' 参数类型编号不可分配给参数类型字符串 | 未定义类型编号不可分配给类型字符串 - Argument type number is not assignable to parameter type string | undefined Type number is not assignable to type string 参数类型Number | Function不能赋予参数类型Number - Argument type Number is not assignable to parameter type String|Function 类型&#39;string |的参数 编号 string []&#39;不可分配给&#39;string&#39;类型的参数 - Argument of type 'string | number | string[]' is not assignable to parameter of type 'string' 'number' 类型的参数不能使用 ParseInt 分配给'string' 类型的参数 - Argument of type 'number' is not assignable to parameter of type 'string' using ParseInt
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM