Remove && node.children.length > 0 from hasChild.So the hasChild should looks like, hasChild = (_: number, node: FoodNode) => !!node.children;