Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding "public" / "private" in typescript class

Tags:

typescript

In the below type script code , irrespective of whether name is "public" or "private" , java script code that is generated is same.

So my question is, how to decide when the constructor parameter should be public or private ?

// typescript code
class Animal {  
constructor( public name: string) {     
}

}

// generated JS code
var Animal = (function () {
function Animal(name) {
    this.name = name;
}
return Animal;
}());
like image 535
refactor Avatar asked Aug 02 '16 06:08

refactor


3 Answers

The public, private, protected access modifiers, as you have discovered, don't actually affect the final outputted code. What they do affect is the type checking at compile time.

What do they actually do?

As their names suggest, the public and private modifiers limit what can access the class member. Their is also a third modifier in the clan, protected.

The private modifier only allows a class member (variable or method) to be accessed within that class.

The protected modifier allows everything the private modifier does, and also allows other classes that extend that class to use it.

Finally, the public modifier makes it so anything can access the class also has access to the public class property.

For a more in-depth explanation and examples, take a look at the official TypeScript Handbook's explanation.

If it all compiles the same, why should I use the modifiers?!

Using the modifiers will enable the compiler to make sure that your code isn't using things that it shouldn't be using. This is the same reasoning behind using types in the first place, it makes it harder to make mistakes that shouldn't be able to be made in the first place! As an added bonus, if your text editor has TypeScript support, it will also use the access modifiers when showing you autocomplete values for variables and methods.

like image 190
Josiah Nunemaker Avatar answered Oct 03 '22 11:10

Josiah Nunemaker


java script code that is generated is same

They produce the same JavaScript but don't have the same semantics as far as the type is concerned.

The private member can only be accessed from inside the class whereas public can be excessed externally.

More

The differences are covered here : https://basarat.gitbooks.io/typescript/content/docs/classes.html#access-modifiers

Another example

let foo = 123;

will generate the same ES5 as

const foo = 123; 

However in the first case let foo = 123;foo = 456 will compile fine but const foo = 123; foo = 456 will result in a compile time error.

like image 37
basarat Avatar answered Oct 03 '22 10:10

basarat


In ESnext, private class fields are defined using a hash # prefix:

class MyClass {
  a = 1;          // .a is public
  #b = 2;         // .#b is private
  static #c = 3;  // .#c is private and static
  incB() {
    this.#b++;
  }
}

const m = new MyClass();
m.incB(); // runs OK
m.#b = 0; // error - private property cannot be modified outside class

*Note: there’s no way to define private methods, getters and setters.

but a proposal is there https://github.com/tc39/proposal-private-methods

like image 1
kdaShivantha Avatar answered Oct 03 '22 11:10

kdaShivantha