FEWV minutes of TypeScript Classes

» Classes in TypeScript.

  1. TypeScript, a superset of JavaScript, introduces the concept of classes to enable developers to write more structured and maintainable code.

  2. Classes provide a blueprint for creating objects with predefined properties and methods. In this blog post, we will explore how to define properties, methods, and constructors in TypeScript classes, along with code snippets to illustrate the concepts.

» Properties

  1. Properties are the characteristics or data associated with an object.

  2. Class properties are variables defined within a class.

  3. Here’s an example of a TypeScript class with properties:

class Person {
    // Properties
    name: string;
    age: number;
}
  1. In the example above, the Person class has two properties: name and age. The name property has a type of string, while age property has a type of Number.

» Constructor

  1. A constructor is a special method that is called when an object is instantiated from a class. It is used to initialize the properties of the class and perform any necessary setup.

  2. Let’s modify our Person class to include a constructor:

class Person {
    // Properties
    name: string;
    age: number;

    // Constructor
    constructor(name: string, age: number) {
        this.name = name;
        this.age = age;
    }
}
  1. The constructor here accepts a name and age parameters that have a type of string and number respectively and assigns it to the name property.

  2. Within the constructor, we assign the parameter values to the corresponding properties using the this keyword.

» Class Methods

  1. Methods are functions associated with an object, allowing it to perform actions or calculations. In TypeScript classes, methods are defined within the class body and can access properties and other methods of the class.

  2. Let’s add some methods to the Person class:

class Person {
    // Properties
    name: string;
    age: number;

    // Constructor
    constructor(name: string, age: number) {
        this.name = name;
        this.age = age;
    }

    // Class Methods
    greet() {
        console.log(`Hello, ${this.name} your age is ${this.age}.`)
    }
}
  1. In the updated Person class, we added one method: greet(). The greet() method simply logs a message to the console, indicating the name and age of the Person.

» Using the Class

  1. Now let us use this Person class. To use the class we will have to declare a variable.
const person = new Person("John", 25);
  1. Here, we created an instance of the Person class, passing the name “John” and age 25 to the constructor.

  2. Calling the Class Method:

person.greet(); // Output: Hello, John your age is 25.
  1. Here, We then call the greet method on the person instance, which logs a greeting message.

» Conclusion

  1. In this blog post, we delved into the world of classes in TypeScript and learned how to define properties, methods, and constructors.

  2. Properties store the data associated with an object, while methods allow objects to perform actions. The constructor is responsible for initializing the object’s properties. By leveraging classes, TypeScript empowers developers to write structured and reusable code.

  3. Remember, this is just the tip of the iceberg when it comes to working with classes in TypeScript. There are advanced concepts like inheritance, abstract classes, and interfaces that can further enhance your coding experience. So keep exploring and refining your skills to become a TypeScript pro!


I hope this cleared - Classes, it’s properties, Methods and Constructor in TypeScript. Thank You and Happy coding!😊