How to use Interface with Class in TypeScript ? Last Updated : 23 Jan, 2025 Comments Improve Suggest changes Like Article Like Report 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 to an interface, providing concrete implementations for the declared members.These are the following methods: 1. Interface Implemented by ClassIn TypeScript, a class can implement an interface to ensure it adheres to a specific structure defined by the interface.Syntax:class ClassName implements InterfaceName { // Class properties and methods} JavaScript interface Shape { calculateArea(): number; } class Rectangle implements Shape { width: number; height: number; constructor(width: number, height: number) { this.width = width; this.height = height; } calculateArea(): number { return this.width * this.height; } } const rect = new Rectangle(5, 10); console.log(rect.calculateArea()); The Shape interface defines a contract with the calculateArea method.The Rectangle class implements the Shape interface, providing concrete implementations for the calculateArea method.An instance of Rectangle is created with a width of 5 and a height of 10, and the calculateArea method is called to compute the area.Output:502. Multiple Interfaces Implemented by ClassIn TypeScript, a class can implement multiple interfaces, allowing it to adhere to multiple contracts and ensuring it provides implementations for all the specified members.Syntax:class ClassName implements Interface1, Interface2 { // Class properties and methods} JavaScript interface Shape { calculateArea(): number; } interface Color { color: string; } class Circle implements Shape, Color { radius: number; color: string; constructor(radius: number, color: string) { this.radius = radius; this.color = color; } calculateArea(): number { return Math.PI * this.radius * this.radius; } } const circle = new Circle(5, 'red'); console.log(`Color: ${circle.color}`); console.log(`Area: ${circle.calculateArea()}`); The Shape interface declares a calculateArea method, defining a contract for calculating the area of a shape.The Color interface includes a color property, specifying that any implementing class should have a color attribute.The Circle class implements both Shape and Color interfaces, providing concrete implementations for the calculateArea method and the color property.Output:Color: redArea: 78.53981633974483 Comment More infoAdvertise with us Next Article How to use Interface with Class in TypeScript ? G gauravggeeksforgeeks Follow Improve Article Tags : JavaScript Web Technologies TypeScript Similar Reads How to use class syntax in Typescript ? 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 use 2 min read How to Cast Object to Interface in TypeScript ? In TypeScript, sometimes you need to cast an object into an interface to perform some tasks. There are many ways available in TypeScript that can be used to cast an object into an interface as listed below: Table of Content Using the angle bracket syntaxUsing the as keywordUsing the spread operatorU 3 min read When to use interfaces and when to use classes in TypeScript ? TypeScript supports object-oriented programming features like classes and interfaces etc. classes are the skeletons for the object. it encapsulates the data which is used in objects. Interfaces are just like types for classes in TypeScript. It is used for type checking. It only contains the declarat 4 min read How to check interface type in TypeScript ? Typescript is a pure object-oriented programming language that consists of classes, interfaces, inheritance, etc. It is strict and it statically typed like Java. Interfaces are used to define contacts in typescript. In general, it defines the specifications of an entity. Below is an example of an in 2 min read How to Extend an Interface from a class in TypeScript ? In this article, we will try to understand how we to extend an interface from a class in TypeScript with the help of certain coding examples. Let us first quickly understand how we can create a class as well as an interface in TypeScript using the following mentioned syntaxes: Syntax:Â This is the s 3 min read Like