Skip to main content

Small Interfaces

The Interface Segregation Principle says: no type should be forced to implement methods it does not need. Instead of one fat interface with many methods, make several small ones — each type implements only what is true for it.

TypeScript makes small interfaces cheap: types match an interface by shape, so a class satisfies a one-method interface just by having that method. You can define a tiny interface for exactly what your function needs, and every value that fits it already qualifies.


The Fat Interface

A Worker interface with work() and eat() looks harmless — until a robot applies for the job. The robot works fine, but it must implement eat() too, with... something. Nonsense answers like this are the signature of a fat interface.

The robot forced to eat

The robot implements eat() only because the interface demands it. The lunch break prints an answer that should never exist — the class was forced to lie.

Prompt
Write a TypeScript program showing an Interface Segregation violation: a fat Worker interface with work and eat methods, a Human class that does both, and a Robot class forced to implement eat with a nonsense answer. Run a work shift and a lunch break over both to show the robot's forced lie.
Output: TypeScript file and captured output

Split the Interface

Split the fat interface into Worker and Eater. The human implements both; the robot implements only Worker. The work shift takes workers, the lunch break takes eaters — the robot is simply never invited to lunch, and no method lies.

Each type implements only its truth

Two small interfaces instead of one fat one. The robot no longer has an eat() method at all — the type checker keeps it out of the lunch break automatically.

Prompt
Fix the fat interface in TypeScript by splitting it: a Worker interface with work and an Eater interface with eat. Human implements both, Robot only Worker. Run the work shift with both and the lunch break with only the eaters.
Output: TypeScript file and captured output
Each type implements only its truth

Small Pieces Compose

Small interfaces are not a limitation — they combine. When some function really needs both abilities, it composes them into one interface on the spot: Worker plus Eater.

That is the pattern to aim for: define small interfaces, and let whoever needs more glue them together with extends.

Compose when you need both

WorkingEater is built from the two small interfaces. The full-day shift needs both abilities, so only the human qualifies — said once, in the type system.

Prompt
Show TypeScript interface composition: combine the small Worker and Eater interfaces into a WorkingEater interface with extends and write a fullDay function that requires it, so only the Human qualifies. Print the full day of the human.
Output: TypeScript file and captured output