How to use class syntax in Typescript ? Last Updated : 16 Mar, 2022 Comments Improve Suggest changes Like Article Like Report Classes: The class keyword was introduced in ES2015. TypeScript fully supports 'class' keyword. classes are a template for creating objects. A Class is a user defined data-type which has data members and member functions. Data members are the data variables and member functions are the functions used to manipulate these variables and together these data members and member functions defines the properties and behavior of the objects in a Class. Syntax: class class_name { field; methods; }In the above syntax of class in TypeScript we just use class keyword along with class_name (you can give any name to class as per your choice or as per camelCase) and use curly braces to define fields (variables) and methods(functions). Example 1: In the below example we create class (Gfg) and declare fields along with constructor and function or method and by creating object of that Gfg class we are accessing fields and methods through that object. JavaScript class Gfg { // Fields gfgid: number; gfgrole: string; // Constructor call constructor(id: number, role: string) { this.gfgid = id; this.gfgrole = role; } // Method getGrade(): string { return "Employee track record is A+"; } } const gf = new Gfg(10, "front-end developer"); console.log(`Id of gfg employee is :${gf.gfgid}`); console.log(`role of gfg employee is :${gf.gfgrole}`); gf.getGrade(); Output: Id of gfg employee is : 10 role of gfg employee is : front-end developer Employee track record is A+The example declare a Gfg class which has two fields that is gfgid and gfgrole and a constructor which is special type of function which is responsible for variable or object initialization. Here it is parameterized constructor(already having the parameters). And this keyword which refers to the current instance of the class. getGrade() is a simple function which returns a string. Example 2: In the below example we create class Geeks and declare function or method and by creating object of that Geeks class we are accessing method of class through that object. JavaScript class Geeks{ getName() : void { console.log("Hello Geeks"); } } const ad = new Geeks() ad.getName(); Output: Hello Geeks Reference: https://www.typescriptlang.org/docs/handbook/classes.html Comment More infoAdvertise with us Next Article How to use class syntax in Typescript ? D devendrasalunke Follow Improve Article Tags : JavaScript Web Technologies Geeks Premier League TypeScript Geeks-Premier-League-2022 Similar Reads How to use Interface with Class in TypeScript ? In TypeScript, interfaces define the structure that classes must adhere to, ensuring consistent object shapes and facilitating type-checking.Interfaces declare properties and methods without implementations, serving as contracts for classes to implement.Classes use the implements keyword to adhere t 3 min read How to use express in typescript ? In this article, we will see how to use Express in TypeScript. The TypeScript is a superset of JavaScript that provides type notation with the JavaScript, so we can handle our server, and very scalable for the future. Express is web framework helps to create server-side handling with the help of Nod 2 min read How to use Type Guards in TypeScript ? Here are the methods to use type guards in TypeScript:1. Using typeof Type GuardsThe typeof operator checks the type of a variable, primarily for primitive types like string, number, boolean, etc. JavaScriptfunction processValue(value: string | number) { if (typeof value === 'string') { console.log( 3 min read How to use getters/setters in TypeScript ? In TypeScript, getters and setters provide controlled access to class properties, enhancing encapsulation and flexibility.Getters allow you to retrieve the value of a property with controlled logic.Setters enable controlled assignment to properties, often including validation or transformations.Java 5 min read How to use property decorators in TypeScript ? Decorators are a way of wrapping an existing piece of code with desired values and functionality to create a new modified version of it. Currently, it is supported only for a class and its components as mentioned below: Class itselfClass MethodClass PropertyObject Accessor ( Getter And Setter ) Of C 4 min read Like