'Typescript error while using .find function : This expression is not callable
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)
Solution 1:[1]
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})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |