Open this example in the interactive library
Source code
type Tree = { label: string; children: Tree[] };
const root: Tree = {
label: "src",
children: [
{ label: "app", children: [{ label: "ui", children: [] }] },
{ label: "lib", children: [] },
],
};
console.log(
(() => {
function leaves(t: Tree): string[] {
return t.children.length ? t.children.flatMap(leaves) : [t.label];
}
return leaves(root);
})(),
);
Sample output
[ 'ui', 'lib' ]
