Member-only story
TypeScript Tips: Always Require Member Accessors

A member accessor is a keyword to specify the accessibility (public
, private
, or protected
) of your properties and methods within a class to other classes.
public
members can be used by anything. private
members can only be used within this current class. And protected
members can be used by this class and any others that extend it.
By default, you can leave off the accessor completely, and TypeScript will assume it’s public.
class Car {
drive() {}
}
So if the public
is implicit, then why do we need to add verbosity by adding the public keyword? Simple. In my experience, developers are lazy and forgetful. They will never specify the accessor keyword on any method, including ones that should only be used locally.
class Car {
// This should be public
drive() {
this.fireSparkPlug();
}
// But this really should not be public
fireSparkPlug() {}
}
Side Note: I know nothing about how cars work!
Sure, we could leave off the public
and only set the accessor when it is public
or private
like this:
class Car {
drive() {
this.fireSparkPlug();
}
private fireSparkPlug() {}
}