简体   繁体   中英

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] }'

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:

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. 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. 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 . Instead you are calling it with a number and you get the error that you've posted here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM