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 info G gauravggeeksforgeeks Follow Improve Article Tags : TypeScript Explore TypeScript Tutorial 8 min read TypeScript BasicsIntroduction to TypeScript 3 min read Difference between TypeScript and JavaScript 4 min read How to install TypeScript ? 3 min read Hello World in TypeScript 2 min read How to execute TypeScript file using command line? 2 min read Variables in TypeScript 6 min read What are the different keywords to declare variables in TypeScript ? 4 min read Identifiers and Keywords in TypeScript 2 min read TypeScript primitive typesData types in TypeScript 3 min read TypeScript Numbers 4 min read TypeScript String 4 min read Explain the concept of null and its uses in TypeScript 3 min read TypeScript Object typesWhat are TypeScript Interfaces? 4 min read TypeScript class 4 min read How enums works in TypeScript ? 4 min read TypeScript Tuples 4 min read TypeScript other typesWhat is any type, and when to use it in TypeScript ? 3 min read How to Create an Object in TypeScript? 4 min read What is an unknown type and when to use it in TypeScript ? 3 min read Explain the purpose of never type in TypeScript 3 min read TypeScript combining typesTypeScript Union 3 min read What are type aliases and how to create it in Typescript ? 3 min read TypeScript AssertionsExplain Type assertions in TypeScript 3 min read TypeScript FunctionsHow to write a function in Typescript ? 4 min read How to achieve function overloading in TypeScript ? 2 min read Explain the arrow function syntax in TypeScript 2 min read TypeScript toPrecision() Function 1 min read TypeScript toFixed() Function 2 min read TypeScript toLocaleString() Function 2 min read TypeScript toString() 1 min read TypeScript interfaces and aliasesWhat are TypeScript Interfaces? 4 min read What are type aliases and how to create it in Typescript ? 3 min read TypeScript classesHow to Extend an Interface from a class in TypeScript ? 2 min read How to Create an Object in TypeScript? 4 min read How to use getters/setters in TypeScript ? 5 min read TypeScript Inheritance 3 min read When to use interfaces and when to use classes in TypeScript ? 4 min read Generics Interface in typescript 5 min read How to use property decorators in TypeScript ? 4 min read Like