Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
Difference between Flow and TypeScript
Next article icon

Difference between interfaces and classes in TypeScript

Last Updated : 16 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will see what is the Difference between "interface" and "Classes" in TypeScript.

Interface: Interface is the virtual structure that is used for type-checking. In TypeScript we use interface keyword to create the new interface with identity. It create the structure for the same datatype. Interface is the structure which define the properties and method for object with name and type.

Syntax:

interface New_Interface{ // This is interface Body }

Features:

  • It has loose coupling.
  • It supports multiple Inheritance.

Example 1:

JavaScript
// Interface for Class interface ForClass {     readonly var1:string;                }  let newclass: ForClass = {var1:"Interface"}; console.log(newclass); 

Output:

{ var1: 'Interface' }

Example 2:

JavaScript
// Interface for Object with extends function  interface ForObj {     First: string }  interface forNewObj extends ForObj {     Second: number }  let newArray: forNewObj = {     First: "Interface for Object",     Second: 2 };  console.log(newArray); 

Output:

{ First: 'Interface for Object', Second: 2 }

Classes: They are the skeleton of objects with its use we implement objects. In TypeScript, we use class Keyword to create the constructor for the object. It can have properties, methods and variables.

Syntax:

class geeks {      // Class property and methods      // are created here  }

Features:

  • In classes it support member visibility.
  • It supports member overriding.
  • it supports inheritance.

Example:

JavaScript
class Geeks {           name : string ;     articles: number ;      cp_problems: number;      // Constructor of class     constructor(name:string, articles: number, cp_problems: number) {         this.name = name;         this.articles = articles;         this.cp_problems = cp_problems;     }                       // About object      About() : void {         console.log("Name of Geeks: " + this.name );         console.log("No. of articles by Geeks: "             + this.articles);         console.log("No. of cp problems sol by Geeks: "             + this.cp_problems)     } } var geek1 = new Geeks("Abhinav", 87, 560); geek1.About(); 

Output:

Name of Geeks: Abhinav  No. of articles by Geeks: 87  No. of cp problems sol by Geeks: 560

The difference between interface and classes are below:

Interface

Classes

We can Create the interface with the use of the interface keyword.

i.e interface Interface_Name{   \\ Interface Body }

We can create the class with class keyword.

i.e class Class_Name{ \\ Class Body }

The interfaceblueprint is mainly the Type structure of object. i.e It is object with only defining the type of parameter inside.Class is the blueprint of the object i.e.the create purposes class is how we implement the object of our code. 
It is used for type checking purpose. Use of interface if TypeScript language is mainly focused on the checking the type of parameters in object.Classes in Types script is used to made the object for something. It is used for implementing the object.
We cannot create the instance of interface with new in typescript. It means that we cannot create the copy of instance in Typescript.We can create a new instance of the class in TypeScript. It means that we can create the copy of class with new keyword.
Interface is virtual structure. Means it only present in TypeScript code not in TypeScript compiled JavaScript code. It always exists in code after the compilation of TypeScript to JavaScript. 

Next Article
Difference between Flow and TypeScript

S

satyam00so
Improve
Article Tags :
  • Difference Between
  • JavaScript
  • Web Technologies
  • JavaScript-Questions
  • Web Technologies - Difference Between

Similar Reads

  • What is the difference between interface and type in TypeScript ?
    In TypeScript, both interface and type are used to define the structure of objects, but they differ in flexibility and usage. While interface is extendable and primarily for object shapes, type is more versatile, allowing unions, intersections, and more complex type definitions. Type in TypeScriptTh
    4 min read
  • Difference between TypeScript and Dart language
    TypeScript: It is an open-source pure object-oriented programming and compiled language which is developed and maintained by Microsoft. It has been influenced by JavaScript, Java, C#. It is C-style syntax and it can optionally trans-compile into JavaScript and it used to develop web and mobile appli
    2 min read
  • Difference between ELM and Typescript
    ELM: Elm is a purely functional domain-based programming language used for front-end web development. Its syntax is different from what we have been doing in other coding languages. There are no loops and has lots of arrows and triangles. When it comes to which programming language should we learn f
    5 min read
  • TypeScript Differences Between Type Aliases and Interfaces Type
    In TypeScript, both type aliases and interfaces are used to define custom types, but they have distinct differences and use cases. Type Aliases: Allow the definition of types with a custom name, applicable to primitives, unions, tuples, and more complex types. Interfaces: Primarily used for defining
    4 min read
  • Difference between Flow and TypeScript
    1. Flow : Flow is developed and maintained by Facebook. It is a static type checker, designed to quickly find errors in JavaScript applications. Nothing more, nothing less. It's not a compiler, but a checker. It can work without any type of annotations and it is very good at inferring types. To enab
    2 min read
  • Difference Between Package and Interface in Java
    In Java, packages and interfaces play crucial roles in organizing and structuring code. They serve different purposes and are used in distinct contexts. In this article, we will learn the concepts of the packages and interfaces in Java. syntax provides examples for each and then presents a table hig
    2 min read
  • Difference Between valueof and keyof in TypeScript
    In TypeScript, valueOf() retrieves the primitive value of an object, useful for accessing inherent data types. Conversely, keyof extracts keys as a union type, crucial for type-safe property referencing and dynamic type handling in TypeScript's type system. ValueOf() methodThe valueOf() method is a
    2 min read
  • Difference Between instanceof and isPrototypeOf() in JavaScript
    In JavaScript, understanding object relationships and type checks is crucial for the effective programming. The Two methods that facilitate these checks are instanceof and isPrototypeOf(). While both are used to the determine the type or prototype relationships in the JavaScript they operate differe
    2 min read
  • Differences between Interface and Integration Testing
    Interface Testing: Interface Testing is a type of software testing type that checks the proper communication between two different software systems. Interface is the connection that integrates two components. The interface could be anything like APIs, web services etc. Testing of these connecting in
    2 min read
  • Difference between TypeScript and JavaScript
    Ever wondered about the difference between JavaScript and TypeScript? If you're into web development, knowing these two languages is super important. They might seem alike, but they're actually pretty different and can affect how you code and build stuff online. In this article, we'll break down the
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences