Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

getting type error when I try to assign interface to a property

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading