[Solved] Error in typescript function- Function lacks ending return statement and return type does not include ‘undefined’ [closed]


It seems that the Typescript’s compiler does not understand that if the length of the array is different from zero, it will surely enter the loan and return a value.

So, the simple soultion is this:

  private createBreadcrumbs(route: ActivatedRoute, url: string = '#', breadcrumbs: MenuItem[] = []): MenuItem[] {
    const children: ActivatedRoute[] = route.children;

    for (const child of children) {
      const routeURL: string = child.snapshot.url.map(segment => segment.path).join("https://stackoverflow.com/");
      if (routeURL !== '') {
        url += `/${routeURL}`;
      }

      const label = child.snapshot.data[BreadcrumbComponent.ROUTE_DATA_BREADCRUMB];
      if (label !== null || label !== undefined) {
        breadcrumbs.push({label, url});
      }

      return this.createBreadcrumbs(child, url, breadcrumbs);
    }

    // if (children.length === 0)
    return breadcrumbs;
  }

0

solved Error in typescript function- Function lacks ending return statement and return type does not include ‘undefined’ [closed]