Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript classes: Is explicit 'public' modifier a best-practice?

Tags:

typescript

In TS, the default access level for a class member is public unless anything else is specified. Even so, is it considered a best-practice to use the public modifier anyway? If nothing else to make the code more obvious?

like image 885
csvan Avatar asked Apr 29 '16 17:04

csvan


People also ask

Should I use public in TypeScript?

In TypeScript it's public, obviously. If you happen to be using C# and TypeScript in the same project, or just in parallel, I would recommend going with explicit access modifiers, just for the sake of clarity.

What is the difference between the protected and private TypeScript property modifiers?

TypeScript provides three access modifiers to class properties and methods: private , protected , and public . The private modifier allows access within the same class. The protected modifier allows access within the same class and subclasses. The public modifier allows access from any location.

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.

Are TypeScript methods public by default?

In Typescript, by default, the visibility of all properties or methods in Typescript classes is “public“. A method with which is public can be accessed from anywhere, it has no restrictions.


3 Answers

This is a strongly subjective topic to which no perfect answer exists, IMO. However, I'd say a strong factor in settling on an answer is whether you are using other languages in parallel, and if there is a difference in default accessor modifiers between TypeScript and those other languages.

Take C#, for example. In C#, every property and field without an explicit access modifier is private. In TypeScript it's public, obviously.

If you happen to be using C# and TypeScript in the same project, or just in parallel, I would recommend going with explicit access modifiers, just for the sake of clarity.

like image 198
John Weisz Avatar answered Oct 15 '22 14:10

John Weisz


I personally, do like to list it every time. Of course it's just a matter of personal preference. If you do want to, and you use tslint, there is an option to force explicit visibility every time.

member-access: true
like image 43
rgvassar Avatar answered Oct 15 '22 15:10

rgvassar


As other answers have stated, this is a matter of preference (I prefer the leaner version).

If you use parameter properties though, the explicit public access modifier is compulsory in order to create and initialize an instance member from the given parameter.

class Octopus {
    readonly numberOfLegs: number = 8;
    constructor(public name: string, ink: boolean) { }
}

const o = new Octopus("Lui", true)
o.name // works
o.ink // error
like image 30
ford04 Avatar answered Oct 15 '22 15:10

ford04