I'm using typescript in my Angular project trying to assign IInfoPage interface to data
export interface IInfoPage {
href: string;
icon: string;
routing: boolean;
order: number;
styleType: string;
}
public pageData: IInfoPage;
this.configService.PageData.subscribe((res) => {
this.pageData = res.SubMenu.find(data => location.pathname.includes('path'));
})
But as I see this.pageData alerts and gives me this error:
Type 'ISubMenuType | undefined' is not assignable to type 'IInfoPageSubData'. Type 'undefined' is not assignable to type 'IInfoPageSubData'.
So How can I solve this issue?
CodePudding user response:
Array.find()
can always return undefined
, so you'll need to check whether or not find
has returned a result.
const result = res.SubMenu.find(data => location.pathname.includes('path'));
if(result)
this.pageData = result;