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

前端 未结 4 1872
一个人的身影
一个人的身影 2020-12-16 10:30

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 publi

4条回答
  •  余生分开走
    2020-12-16 11:08

    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.

提交回复
热议问题