Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of the public access modifier for classes in Typescript?

Tags:

typescript

What is the purpose of the public access modifier for classes in Typescript?

What is the difference between

export class Thing {

  public doStuff(){}

}

and

export class Thing {

  doStuff(){}

}

It seems that the standard tsc JS output is exactly the same...

like image 459
geejay Avatar asked Oct 07 '16 05:10

geejay


People also ask

What is access modifier in TypeScript?

The access modifier increases the security of the class members and prevents them from invalid use. We can also use it to control the visibility of data members of a class. If the class does not have to be set any access modifier, TypeScript automatically sets public access modifier to all class members.

What is use of public access modifier?

The public access modifier is the direct opposite of the private access modifier. A class, method or variable can be declared as public and it means that it is accessible from any class. Public access modifier can be likened to a public school where anyone can seek admission and be admitted.

What does public do in TypeScript?

Public - By default, members (properties and methods) of the TypeScript class are public - so you don't need to prefix members with the public keyword. Public members are accessible everywhere without restrictions even from the multiple level sub-classes without any compile errors.

What is the default access modifier for members of a class in TypeScript?

TypeScript has two access modifiers – public and private. By default the members are public but you can explicitly add a public or private modifier to them.


3 Answers

Two reasons for using public, one subjective/style, the other objective:

  1. It's in-code documentation. Leaving off an access modifier could be taken as your having forgotten it. (This is the subjective/style reason.)

  2. If you want to use the shorthand initialization feature TypeScript adds to class constructors, you need the public to tell TypeScript to do that.

Here's an example of #2:

class Example {
    constructor(public name: string) {
//              ^^^^^^---------------- This is what triggers the feature
    }
}
const e = new Example("Joe");
console.log(e.name); // "Joe"

(On the playground.)

like image 110
T.J. Crowder Avatar answered Nov 15 '22 08:11

T.J. Crowder


Your sample code means exactly the same in TypeScript. When you don't put a modifier public, private or protected on your member definition then TypeScript will choose the default one which is public.

That not make sense for Javascript because TypeScript transpiler will generate exactly the same code. But those modifiers will help you to encapsulate your class members and make them :

  • accessible outside of the class : public
  • only accessible in the class only : private
  • accessible in the class and the derived classes: protected

Again the generated Javascript will not contain the modifiers. That means that if someone get your Javascript code it will access to the members even if you defined them as private. The modifiers are only helpful in TypeScript and help developers to not access some class members.

like image 40
CodeNotFound Avatar answered Nov 15 '22 08:11

CodeNotFound


It's just an explicit way to set the method to be public. By default all class methods / properties in TypeScript are of type public, but you can as well note this in your code for clarity.

You can read more about it in the Handbook, here is an excerpt :

Public by default

In our examples, we’ve been able to freely access the members that we declared throughout our programs. If you’re familiar with classes in other languages, you may have noticed in the above examples we haven’t had to use the word public to accomplish this; for instance, C# requires that each member be explicitly labeled public to be visible. In TypeScript, each member is public by default.

You may still mark a member public explicitly.

like image 39
drinchev Avatar answered Nov 15 '22 09:11

drinchev