Estoy usando mecanografiado en mi proyecto Angular tratando de asignar la interfaz IInfoPage a los datos
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')); })Pero cuando veo this.pageData alertas y me da este error:
Type 'ISubMenuType | undefined' is not assignable to type 'IInfoPageSubData'. Type 'undefined' is not assignable to type 'IInfoPageSubData'.Entonces, ¿cómo puedo resolver este problema?
Array.find() siempre puede devolver undefined , por lo que deberá verificar si find ha devuelto un resultado o no.
const result = res.SubMenu.find(data => location.pathname.includes('path')); if(result) this.pageData = result;