What is the syntax of creating classes in TypeScript?

Classes in TypeScript are defined as a blueprint for creating objects to utilize the properties and methods defined in the class. Classes are the main building block of the Object Oriented Programming in TypeScript. The classes in TypeScript can be created in the same way as we create them in Vanilla JavaScript using the class keyword. Classes help us to implement the four principles of OOPs Inheritance, Polymorphism, Encapsulation, and Abstraction. The classes can be defined with multiple properties and methods with different visibilities.

Syntax:

class className {
// Class properties and methods
}

Example: The below code creates and implements a simple class in TypeScript.

Javascript




class myClass {
    name: string;
    est: number;
    constructor(name, est) {
        this.name = name;
        this.est = est;
    }
}
 
const myObj: myClass =
    new myClass("w3wiki", 2009);
console.log(myObj)


Output:

{
name: "w3wiki",
est: 2009
}

Contact Us