Typescript Abstract Property

In this blog post, we examine how class definitions work in TypeScript:

Static Properties; Abstract Classes; TypeScript Simple Class Example. Let's create a simple Employee class with the following. Unlike an interface, an abstract class may contain implementation details for its members. The abstract keyword is used to define abstract classes as well as abstract methods within an abstract class. As you can see, the subclass properties aren’t set until AFTER the base constructor is called. Ryan Cavanaugh from Microsoft explains: This is the intended behavior. The base class initialized properties are initialized. The base class constructor runs. The derived class initialized properties are initialized. Code language: TypeScript (typescript) Typically, an abstract class contains one or more abstract methods. An abstract method does not contain implementation. It only defines the signature of the method without including the method body. TypeScript 3.0.3 ui-button ui-button Class Implementing Interfaces Select All Download.

  • First, we take a quick look at the features of class definitions in plain JavaScript.
  • Then we explore what additions TypeScript brings to the table.

Table of contents:

Cheat sheet: classes in plain JavaScript #

This section is a cheat sheet for class definitions in plain JavaScript.

Basic members of classes #

Modifier: static#

Modifier: # (private) #

Warning:

Typescript virtual property

Modifiers for accessors: get (getter) and set (setter) #

There are two kinds of accessors: getters and setters.

Modifier for methods: * (generator) #

Modifier for methods: async#

Computed class member names #

Comments:

  • The main use case for this feature is symbols such as Symbol.iterator. But any expression can be used inside the square brackets.
  • We can compute the names of fields, methods, and accessors.
  • We cannot compute the names of private members (which are always fixed).

Combinations of modifiers #

Fields:

Methods:

levelaccessorasyncgeneratorvisibility
(prototype)
(prototype)get
(prototype)set
(prototype)async
(prototype)*
(prototype)async*
(prototype-associated)#
(prototype-associated)get#
(prototype-associated)set#
(prototype-associated)async#
(prototype-associated)*#
(prototype-associated)async*#
static
staticget
staticset
staticasync
static*
staticasync*
static#
staticget#
staticset#
staticasync#
static*#
staticasync*#

Limitations of methods:

  • Accessors can’t be async or generators.

Under the hood #

It’s important to keep in mind that with classes, there are two chains of prototype objects:

  • The instance chain which starts with an instance.
  • The static chain which starts with the class of that instance.

Consider the following JavaScript (not TypeScript!) example:

The two prototype chains look as follows:

More information #

  • Public fields, private fields, private methods/getters/setters (blog post)
  • All remaining JavaScript class features (chapter in “JavaScript for impatient programming”)

Non-public data slots in TypeScript #

By default, all data slots in TypeScript are public properties. There are two ways of keeping data private:

Typescript abstract static
  • Private properties
  • Private fields

We’ll look at both next.

Note that TypeScript does not currently support private methods.

Private properties #

Any property can be made private by prefixing it with the keyword private (line A):

We now get compile-time errors if we access that property in the wrong scope (line A):

However, private doesn’t change anything at runtime. There, the property .name is indistinguishable from a public property:

We can also see that private properties aren’t protected at runtime when we look at the JavaScript code that the class is compiled to:

Private fields #

Since version 3.8, TypeScript also supports private fields:

That code is mostly used the same way as the other version:

However, this time, the data is completely safe. Using the private field syntax outside classes is even a JavaScript syntax error (which is why we have to use eval() in line A, so that the code runs):

The compilation result is much more complicated now:

This code uses a common technique for keeping instance data private:

  • Each WeakMap implements one private field.
  • It associates instances with private data.

Typescript Abstract Property

For more information on this technique, see “JavaScript for impatient programmers”.

Private properties vs. private fields #

  • Downsides of private properties:
    • We can’t reuse the names of private properties in subclasses (because the properties aren’t private at runtime).
    • No protection at runtime.
  • Upside of private properties:
    • Clients can circumvent the protection and access private properties. This can be useful if someone needs to work around a bug. In other words: Being completely protected has pros and cons.

Protected properties #

Private properties can’t be accessed in subclasses (line A):

We can fix the previous example by switching from private to protected in line A (we also switch in line B, for consistency’s sake):

Private constructors #

We can also make constructors private. That is useful when we have static factory methods and want clients to always use those methods, never the constructor directly. Static methods can access private instance members, which is why the factory methods can still use the constructor.

In the following code, there is one static factory method DataContainer.create(). It sets up instances via asynchronously loaded data. Keeping the asynchronous code in the factory method enables the actual class to be completely synchronous:

In real-world code, we would use fetch() or a similar Promise-based API to load data asynchronously in line A.

Right now, the private constructor prevents DataContainer from being subclassed. If we want to allow subclasses, we can use protected.

Typescript Abstract Property

Initializing instance properties #

Strict property initialization #

If the compiler setting --strictPropertyInitialization is switched on (which is the case if we use --strict), then TypeScript checks if all declared instance properties are correctly initialized:

  • Either via assignments in the constructor:

  • Or via initializers for the property declarations:

However, sometimes we initialize properties in a manner that TypeScript doesn’t recognize. Then we can use exclamation marks (definite assignment assertions) to switch off TypeScript’s warnings (line A and line B):

Example: setting up instance properties via objects #

Typescript Class Type

In the following example, we also need definite assignment assertions. Here, we set up instance properties via the constructor parameter props:

Notes:

  • In line B, we initialize all properties: We use Object.assign() to copy the properties of parameter props into this.
  • In line A, the implements ensures that the class declares all properties that are part of interface CompilerErrorProps.

Making constructor parameters public, private, or protected#

If we use the keyword public for a constructor parameter, then TypeScript does two things for us:

  • It declares a public instance property with the same name.
  • It assigns the parameter to that instance property.

Therefore, the following two classes are equivalent:

If we use private or protected instead of public, then the corresponding instance properties are private or protected (not public).

Abstract classes #

Two constructs can be abstract in TypeScript:

  • An abstract class can’t be instantiated. Only its subclasses can – if they are not abstract, themselves.
  • An abstract method has no implementation, only a type signature. Each concrete subclass must have a concrete method with the same name and type signature as that abstract method.
    • If a class has any abstract methods, it must be abstract, too.

Typescript Abstract Class Method

The following code demonstrates abstract classes and methods.

On one hand, there is the abstract superclass Printable and its helper class StringBuilder:

Typescript Abstract Property Appraiser

On the other hand, there are the concrete subclasses Entries and Entry:

Typescript Abstract Property Default Value

And finally, this is us using Entries and Entry:

Notes about abstract classes:

Property

Typescript Abstract Property Examples

  • They can be seen as interfaces with bundled implementations.
  • However, a class can only extend one abstract superclass but implement multiple interfaces.
  • “Abstractness” only exists at compile time. At runtime, abstract classes are normal classes and abstract methods don’t exist (due to them only providing compile-time information).
  • Abstract classes can be seen as templates where each abstract method is a blank that has to be filled in (implemented) by subclasses.