user1177227
user1177227

Reputation: 97

Why does typescript show an error for my Array.filter parameters

I am using Array.prototype.filter to return specific entries from within an object, however typescript is giving me an error that "Argument of type '(current: currentInterface) => boolean' is not assignable to parameter of type '(value: [string, {}], index: number, array: [string, {}][]) => any'. Types of parameters 'current' and 'value' are incompatible."

current is an object that contains entries that I want to filter.

How can I get this to work, and why does it not work currently?

It seems that it expects current to be a string, however in normal JS this is not required.

    interface currentInterface {
        firstName: string,
        lastName: string,
        [key: number]: any
    }

    //filter matching customers from list of customers by search term
    const matchingCustomers = Object.entries(state.customers).filter((current: currentInterface) => {
        let firstname = current[1].firstName.toLowerCase();
        let lastname = current[1].lastName.toLowerCase();
        let searchTerm = action.payload.searchTerm.toLowerCase();
        return firstname === searchTerm || lastname === searchTerm;
    });


Argument of type '(current: currentInterface) => boolean' is not assignable to parameter of type '(value: [string, {}], index: number, array: [string, {}][]) => any'.
  Types of parameters 'current' and 'value' are incompatible.
    Type '[string, {}]' is missing the following properties from type 'currentInterface': firstName, lastNamets(2345)

Upvotes: 0

Views: 829

Answers (1)

baao
baao

Reputation: 73221

Because Object.entries returns an array of arrays, you are assigning a single interface though.

From your code, it seems like you where looking for Object.values instead

Upvotes: 1

Related Questions