Reputation: 71
Env:
typescript 4.5.5
Errors
This expression is not callable.
Each member of the union type '{ <S extends User>(predicate: (this: void, value: User, index: number, obj: User[]) => value is S, thisArg?: any): S | undefined; (predicate: (value: User, index: number, obj: User[]) => unknown, thisArg?: any): User | undefined; } | { ...; }' has signatures, but none of those signatures are compatible with each other.ts(2349)
Upvotes: 1
Views: 212
Reputation: 4330
Currently, the users
definition states it is either Array of User
or Array of string.
You can change the definition to const users: Array<User | string> = res.data;
.
This way while looping will be aware that the element inside an array can be both User
or string.
Or if in your case you are sure that the users array can either be an array of User
or string
but not both, then you can keep the definition of users: User[] | string[]
, but trick Typescript while .find
like (users as Array<User | string>).find((x: User | string) => {// find logic})
Upvotes: 2