I declare this to recieve options paramas value where value can be single string or other object like: options?: string[] | IServiceDetail[] | IServiceAccordion[];
But when I am trying to map above objects getting error: Property 'title' does not exist on type 'string | IServiceAccordion | IServiceDetail'. Property 'title' does not exist on type 'string'. enter image description here
return options?.map( (category: string | IServiceDetail | IServiceAccordion) => { return ( <Text key={category.title}
CodePudding user response:
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}
/>
CodePudding user response:
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 null
}