Open In App

What is the syntax of creating classes in TypeScript?

Last Updated : 23 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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("GeeksforGeeks", 2009);
console.log(myObj)


Output:

{
name: "GeeksforGeeks",
est: 2009
}

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads