
TypeScript 4 Design Patterns and Best Practices
By :

Composite is a pattern that offers an alternative way to define hierarchies of objects without using inheritance. Again, you want a pattern that avoids inheritance as much as possible because inheritance has many drawbacks in practice. This is one more case against it.
One analogy of this pattern is a company having different types of employee roles, forming a pyramid. Each person is an employee but they have different responsibilities and you can traverse the hierarchy from top to bottom. Using Composite, you want to define objects similar in nature and type, but without attaching too much business logic and behavior to each of them. You want to allow the clients to decide what to perform with this composition of objects. This allows the clients to treat all objects in the hierarchy uniformly.
There are many reasons why you want to use this pattern and we especially recommend the following ones: