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 ? 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 How to Create Nested Classes in TypeScript ? In TypeScript, you can create nested classes using different methods. We will discuss about three different approaches to creating nested classes in TypeScript. These are the approaches: Table of Content By defining nested classes inside a classBy using the namespacesBy using the modulesBy defining 3 min read How to Use Extension Methods in TypeScript ? Typescript developers in general face situations where they need to add functionality to existing classes. The extension method in typescript helps in extending the functionality of predefined classes or third-party libraries without modifying the source code. In Simple words, Extension methods in T 3 min read How to Declare Variables in TypeScript ? In TypeScript, a variable can be defined by specifying the data type of the value it is going to store. It will store the values of the specified data type only and throws an error if you try to store the value of any other data type. You can use the colon syntax(:) to declare a typed variable in Ty 2 min read How to use Class in Node ? In Node, classes function as templates for creating objects in object-oriented programming, encapsulating both data and behavior. They provide a structured and reusable approach to defining and instantiating objects within a JavaScript program.We will discuss the following two approaches to define a 3 min read How to Create an Object in TypeScript? TypeScript object is a collection of key-value pairs, where keys are strings and values can be any data type. Objects in TypeScript can store various types, including primitives, arrays, and functions, providing a structured way to organize and manipulate data.Creating Objects in TypescriptNow, let 4 min read Like