React-Typescript TypeScript错误:此表达式不可调用

发布时间:2020-07-07 17:27

更改文件以使用打字稿。学习打字稿。

我将签名更改为

export default function PostForm( post: PostInterface, username: string, posts: PostInterface[], setPosts: PostInterface[], setPostMessage:string ) {
...
const newPost = { title, content, author: username, slug, created };
setPosts([newPost, ...posts])

但是现在我遇到setPosts的错误:

TypeScript error: This expression is not callable.
  Type 'PostInterface[]' has no call signatures.  TS2349

vScode说this expression is not callable

回答1

setPosts的类型为PostInterface[],换句话说,setPosts应该是PostInterface的数组。您可能希望setPosts的类型是某种函数。例如:

setPosts: (newPosts: PostInterface[]) => PostInterface[]
// OR, if setPosts doesn't return anything
setPosts: (newPosts: PostInterface[]) => void