I have defined an Interface
, created an array of type Interface
and am now attempting to use .indexOf
, a method of an Array, and I am getting IDE error complaints that make no sense to me. Hoping someone here may be able to lend a thought to solve this pickle.
export interface IAddress {
name: string,
registrationId: number
}
let friends: IAddress[];
// assume friends has a few elements...
let index = friends.indexOf((friend: IAddress) => {
return !!(friend.name === 'some name');
});
Argument of type '(friend: IAddress) => boolean' is not assignable to parameter of type 'IAddress'.
Type '(friend: IAddress) => boolean' is missing the following properties from type 'IAddress': registrationId
If I were to remove the :IAddress
from the typed def next to friend:
I see this error instead.
Argument of type '(friend: any) => boolean' is not assignable to parameter of type 'IAddress'.
Type '(friend: any) => boolean' is missing the following properties from type 'IAddress': registrationId
Array.prototype.indexOf()
receives a parameter searchElement
and a second optional parameter fromIndex
.
Updated answer based on @Pixxl comment to use Array.prototype. findIndex() to get index
variable:
const friends: IAddress[];
// assume friends has a few elements...
const index = friends.findIndex((friend: IAddress) => friend.name === 'some name');
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.