A class is the fundamental building block of object-oriented programming languages like C# or Java. TypeScript also provides support for classes and gives developers access to such features as inheritance, encapsulation, and polymorphism.
In TypeScript, a class is an [...]
If you come from a C# background, you may want to add multiple constructors to a TypeScript class. Although TypeScript doesn't support multiple constructors, you can still achieve similar behavior.
In TypeScript, you can achieve a similar behavior to [...]
When using object-oriented programming concepts in TypeScript, often a developer needs to make a class implement an interface. Luckily, this is easy to do.
To make a class implement an interface in TypeScript, you need to use the special [...]
To create a read-only property in TypeScript, you need to use the readonly keyword.
Here is an example of a read-only property inside an interface:
interface IPerson {
readonly name: string;
role: string;
}
This article explains everything about [...]
Sometimes, a TypeScript developer may need to find the class name of an object. Luckily, it is easy to do.
To find the class name of an object at runtime in TypeScript, you can:
Use the constructor property of [...]
If you come from a computer science object-oriented programming background, you may want to utilize some of the design patterns you've always used but in TypeScript. One of the most well-known design patterns is the singleton.
A singleton is [...]
Sometimes, developers need to declare class constants accessible by all the class's methods. Luckily, this is very easy to accomplish in TypeScript.
To declare class constants in TypeScript, you can use the readonly keyword, like so:
class Cat { [...]
TypeScript, just like such languages as C# or Java, provides support for abstract classes. It is a valuable feature since JavaScript doesn't support the abstract keyword. This brings the question of what is an abstract class and how it [...]
TypeScript, just like JavaScript, provides support for static methods, properties, and initialization blocks. This brings the question of what a static class is and how it works in TypeScript.
A static class contains static properties and methods and is [...]
TypeScript is a superset of JavaScript, and just like its counterpart, it supports object-oriented principles such as class inheritance. When a class inherits another class, a developer may want to override a parent class method. Luckily, this is easy [...]