1

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)

Error Screenshot enter image description here

1 Answer 1

2

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})

Sign up to request clarification or add additional context in comments.

2 Comments

If you don't want to manually distribute the types, you can use a little helper type like this: type Ugh<T> = (T extends (infer X)[] ? X : never)[]. Then, you can do something like (users as Ugh<typeof users>).find(...). 🤦 just rename Ugh in your actual code…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.