D
D
Dmitry Samoilov2021-11-08 16:57:44
typescript
Dmitry Samoilov, 2021-11-08 16:57:44

TypeScript throws error: Type cannot be assigned to type 'IntrinsicAttributes & "" & { children?: ReactNode; }", how to fix this?

Hello, I'm doing a small pet project to improve my skills in TS.
I receive data from the server, I seem to be able to type them normally, the posts started to be displayed without errors, but then I decided to put the code with iteration into a separate component, and ts immediately gave an error:

Тип "{ posts: IPost[]; }" не может быть назначен для типа "IntrinsicAttributes & IPost & { children?: ReactNode; }".
  Свойство "posts" не существует в типе "IntrinsicAttributes & IPost & { children?: ReactNode; }"


Error on Posts component with posts={posts} prop:
Main.ts
export const Main: FC= () => {
  const [posts, setPosts] = useState<IPost[]>([]);

  useEffect(() => {
    try {
      const fetchPost = async () => {
        const res = await axios.get('/posts');
        setPosts(res.data);
      };
      fetchPost();
    } catch (error) {
      console.log(error);
    }
  }, []);

  return (
    <>
      <div className='main-container'>
        <NewPosts />
        <PostSort />
        <Posts posts={posts} />
      </div>
    </>
  );
};


Posts.tsx
export const Posts: FC<IPost> = ({ posts }) => {
  return (
    <div className='post-container'>
      {posts.map((post) => (
        <Post key={post._id} post={post} />
      ))}
    </div>
  );
};

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question