I declare this to receive options params value where value can be a single string or another object like options?: string[] | IServiceDetail[] | IServiceAccordion[];
But when I am trying to map the above objects getting an error:
Property 'title' does not exist on type 'string | IServiceAccordion | IServiceDetail'.
Property 'title' does not exist on type 'string'.
return options?.map(
(category: string | IServiceDetail | IServiceAccordion) => {
return (
<Text
key={category.title}
you can check if the type is not a string before accessing the title, because a string is a primitive and does not have any property:
options?.map((category: string | IServiceDetail | IServiceAccordion) => {
if(typeof category !== "string") return <Text key={category.title} />
else return return <Text key={category} />
}
because string is a premitive type. so doesn't have "title" attribute. if you are sure that category is not string and is object and also has "title" attribute, you can use type casting
<Text
key={(category as IServerDetail).title}
/>