+TypeScript example
Entry lets callers read files and folders without distinguishing their concrete types. A file returns its own size; a folder sums its children, keeping recursive aggregation inside Folder.
// Give callers the same read contract for leaves and groups.
abstract class Entry { abstract get bytes(): bigint; }
class FileEntry extends Entry {
constructor(private readonly length: bigint) { super(); }
override get bytes(): bigint { return this.length; }
}
// Read the shared property recursively instead of checking each child’s concrete type.
class Folder extends Entry {
private readonly children: readonly Entry[];
constructor(children: readonly Entry[]) { super(); this.children = [...children]; }
override get bytes(): bigint {
return this.children.reduce((total, child) => total + child.bytes, 0n);
}
}
const entries: Entry[] = [
new FileEntry(7n),
new Folder([new FileEntry(5n), new Folder([new FileEntry(10n), new FileEntry(20n)])]),
];
for (const entry of entries) console.log(entry.bytes.toString());
7
35
Sizes use bigint to keep integer arithmetic, and are converted to strings for output. The constructor copies the child array; this example does not access a filesystem.