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

Output:

{
name: "GeeksforGeeks",
est: 2009
}



Reffered: https://www.geeksforgeeks.org


JavaScript

Related
Explain the use of the any type in TypeScript ? Explain the use of the any type in TypeScript ?
How to implement optional chaining in TypeScript? How to implement optional chaining in TypeScript?
Explain the concept of enums in TypeScript ? Explain the concept of enums in TypeScript ?
How to handle asynchronous operation in TypeScript? How to handle asynchronous operation in TypeScript?
What is the never type in TypeScript? What is the never type in TypeScript?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
11