Here’s a rewritten version of the article:
Unlocking the Power of TypeScript Classes
When it comes to building robust and maintainable applications, TypeScript classes are an essential tool in your toolkit. They provide a blueprint for creating objects with predefined properties and methods, making it easier to write reusable and modular code.
Creating Objects Without the Need for Classes
One of the lesser-known features of TypeScript is its ability to create objects directly without the need for formal class definitions. This can be achieved using object literals, which allow you to define properties and methods in a concise and expressive way.
For example, let’s create a person
object using an object literal:
“
Hello, my name is ${this.name} and I am ${this.age} years old.`);
const person = {
name: 'Jack',
age: 30,
greet() {
console.log(
}
};
person.greet(); // Output: Hello, my name is Jack and I am 30 years old.
“
name
As you can see, we've created an object with propertiesand
age, as well as a
greet()` method that displays a greeting message.
Diving Deeper into TypeScript Classes
Now that we’ve seen how to create objects without classes, let’s take a closer look at the features of a TypeScript class. A class is defined using the class
keyword, and it can contain properties, methods, and even constructors.
Constructors: Initializing Properties with Ease
A constructor is a special method in a class that runs automatically when an instance is created using the new
keyword. In our Person
class, the constructor initializes the name
and age
properties when a new instance is created.
class Person {
constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
greet() {
console.log(<code>Hello, my name is ${this.name} and I am ${this.age} years old.</code>);
}
}
const person1 = new Person('Jack', 30);
const person2 = new Person('Tina', 33);
person1.greet(); // Output: Hello, my name is Jack and I am 30 years old.
person2.greet(); // Output: Hello, my name is Tina and I am 33 years old.
<strong>Class Methods: Defining Behaviors for Objects</strong></p>
<p>A class method is a function inside a class that defines behaviors for the class's objects. In our <code>Person</code> class, the <code>greet()</code> method displays a greeting message when called on objects of the class.</p>
<p><strong>Default Values and Type Inference</strong></p>
<p>One of the convenient features of TypeScript classes is the ability to assign default values to class properties. This means that even if no values are passed when creating an object, the defaults will be used.</p>
<p>“`
class Person {
name: string = ‘John Doe’;
age: number = 25;
greet() {
console.log(Hello, my name is ${this.name} and I am ${this.age} years old.
);
}
}
const person1 = new Person();
person1.greet(); // Output: Hello, my name is John Doe and I am 25 years old.
<strong>Access Modifiers: Controlling Access to Properties and Methods</strong></p>
TypeScript access modifiers control where class properties or methods can be accessed. There are three types of access modifiers: public
, private
, and protected
.
<
p>```
class Person {
public name: string;
private age: number;
constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
greet() {
console.log(<code>Hello, my name is ${this.name} and I am ${this.age} years old.</code>);
}
}
const person1 = new Person('Jack', 30);
console.log(person1.name); // Output: Jack
console.log(person1.age); // Error: Property 'age' is private and only accessible within class 'Person'.
By mastering TypeScript classes, you’ll be able to write more robust, maintainable, and scalable code. Whether you’re building a small application or a large enterprise system, classes are an essential tool in your toolkit.