【发布时间】:2022-01-24 05:39:59
【问题描述】:
这辈子都想不通。
这是代码示例
Parent.tsx
const [placePhotos, setPlacePhotos] = useState<string[]>([])
...
useEffect(() => foo(setPlacePhotos), [])
helper.ts where foo() 存在
type GooglePlaceInfoObject = {
setPlacePhotos?: (photos: Array<string>) => string[]
}
foo({ setPlacePhotos }: GooglePlaceInfoObject) {
if (setPlacePhotos) {
// this returns an array of strings
const photosArray = googlePlaceDetailsObject.photos.map(photo => {
return photo.getUrl({
maxWidth: 320,
maxHeight: 426,
})
})
// error happens here!
setPlacePhotos((oldPlacePhotos: string[]) => [
...oldPlacePhotos,
...photosArray,
])
}
}
使用时出错 setPlacePhotos
Argument of type '(oldPlacePhotos: string[]) => string[]' is not assignable to parameter of type 'string[]'.
Type '(oldPlacePhotos: string[]) => string[]' is missing the following properties from type 'string[]': pop, push, concat, join, and 28 more.
你如何解决这个错误?
【问题讨论】:
标签: reactjs typescript