Understanding TypeScript's private

TypeScript is a structural type system. When we compare two different types, regardless of where they came from, if the types of all members are compatible, then we say the types themselves are compatible.
However, when comparing types that have private and protected members, we treat these types differently. For two types to be considered compatible, if one of the them has a private member, then the other must have a private member that originated in the same declaration. The same applies to protected members.

Understanding protected

The protected modifier acts much like the private modifier with the exception that members declared protected can also be accessed within deriving classes.
A constructor may also be marked protected. This means that the class cannot be instantiated outside of its containing class, but can be extended.

Readonly modifier

You can make properties readonly by using the readonly keyword. Readonly properties must be initialized at their declaration or in the constructor.

Parameter properties

Accessors

Accessors with a get and no set are automatically inferred to be readonly. This is helpful when generating a .d.ts file from your code, because users of your property can see that they can't change it.

Abstract Classes

Abstract classes are base classes from which other classes may be derived. They may not be instantiated directly. Unlike an interface, an abstract class may contain implementation details for its members. The abstract keyword i used to define abstract classes as well as abstract methods within an abstract class.

Constructor functions

When you declare a class in TypeScript, you are actually creating multiple declarations at the same time.
  1. The type of the instance of the class
  1. The constructor function.

typeof [constructor]

{ new(): any }

Using a class as an interface

As we said in the previous section, a class declaration creates two things: a type representing instances of the class and a constructor function. Because classes create types, you can use them in the same places you would be able to use interfaces.

Create a class with a constructor function

Declare the Bird class before creating it.
Using type assertions
 
badge