环境规划署:
typescript 4.5.5
错误
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)
误差截图
发布于 2022-01-25 03:44:24
当前,users
定义声明它要么是User
数组,要么是字符串数组。
您可以将定义更改为const users: Array<User | string> = res.data;
。
这样,循环将注意到数组中的元素既可以是User
,也可以是字符串。
或者,如果您确信用户数组可以是User
或string
的数组,但不能两者兼而有之,那么您可以保留users: User[] | string[]
的定义,但是可以使用技巧类型记录,而.find
则是(users as Array<User | string>).find((x: User | string) => {// find logic})
。
https://stackoverflow.com/questions/70843127
复制相似问题