Why use private classes in Typscript?

Since I haven’t coded in OOP-based languages before, I can’t fully understand it. What do private classes do, why would we want to do something like this? What I read from the sources did not satisfy me, I still do not know why we can use this.

Do you have the source available as to what your referring to?

The term “private class” isn’t commonly used, did you mean private method? A “private class” still is a thing, but isn’t exactly specific to TypeScript, as you could do the same thing with JavaScript using any multitude of patterns to get a similar setup where a class is “private”.

Regardless of if we are talking about a private method or private class, the idea is similar. In that you can have a method that can be used and encapsulated within a specific structure and not be accessible outside of that structure.

The main purpose of this is to allow a programmer to include extra logic within a class/module/block-of-code without allowing external access to said block of code. This can be useful to create “helper functions”, or seperate out your own logic.

Now you might ask why would not just expose it anyways, and you might be right in that there isn’t really much harm it exposing all your code in a class/structure to the “outside world”, and technically since private is a TypeScript keyword, your end JavaScript that is generated can be seen/accessed from the “outside world”, but the main benefit is to keep your class/structure api cleaner to those outside of it.

It might not be a big deal if your class has a few methods, but if it has a lot of methods and not all of them are for “outside consumption”, then your just exposing more of your own code causing bloat to its API. Or worse, you could expose some “inner workings” to your class which allows “the outside world” to modify your class in a way you don’t exactly expect creating bugs.

All of this might seem excessive, and such may be true depending on your doing. It just is worth keeping in mind OOP is a pattern best utilized for larger problems and architecturing your overall system, and not really focused on the “small problems”. Or in-many cases it turns into an overengineered mess that could be better represented in simpler terms, but such is a discussion for another time :smiley:

Suppose you want to define a functionality which other developers can use. Let’s say you provide three functions you want others to use. We call theses functions public because other people can use (call) them. To support the functions you define two more functions (what you might call helper functions as their purpose is to help other functions). You need to declare these two functions as private so others cannot call them. The purpose of these two functions are to support the three public functions. If you don’t make them private, then others can call a helper function and mess up the proper working of the three public functions. By making helper functions private, you avoid others to break your code intentionally or otherwise.
If you develop a module, with many classes, for others to use, you might include private classes, those classes you define just to help the implementation of other classes. These private classes are for internal use only, so they should be declared private.
This is a general concept behind public vs private, common to all OOP languages.

The concepts that you want to look up for further reading on this are “abstraction” and “principle of least privilege”.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.