I am trying to apply types to ReactJS props as implemented in the following interface. AS you can see the type tags is union type
export interface TagInterface {
id: number;
name: string;
}
export interface PostInterface {
tags: TagInterface[] | string[];
}
I have been able to to successfully use this type in a component inside a function as shown
onClick={() => handleClick(post.tags[0] as string)}
Now when i try to implement this type on another component TypeScript is throwing an error
Property 'name' does not exist on type 'string | TagInterface'.
Property 'name' does not exist on type 'string'.
useEffect(() => {
const getPostData = async () => {
const { data: posts }: DataInterface = await getRelatedPosts(
post.tags[0].name // this produces an error
);
setRelatedPosts(posts.results);
};
getPostData();
}, []);
How can I properly set the type from the union type?