TypeScript Class Vs. Interface

Class

TypeScript is a JavaScript language that follows an object-oriented paradigm and offers programming capabilities such as classes, interfaces, polymorphism, and data binding, among others. These features have been supported in TypeScript since ES6 and subsequent versions.

Classes serve as the essential building blocks for developing reusable components. They consist of a collection of objects that share common characteristics. In the context of object-oriented programming (OOP), a class functions as a template or blueprint for generating objects. It represents a logical entity.

In a class definition, we are able to specify the subsequent properties:

Fields: This refers to a variable that is defined within a class. Methods: This signifies an operation that can be performed by the object. Constructors: This function is tasked with setting up the object in memory. Nested class and interface: This indicates that one class may include another class within it.

Syntax to declare a class

In TypeScript, a class can be defined by utilizing the class keyword. The syntax outlined below illustrates how to declare a class.

Example

class <class_name>{    
    field;    
    method;    
}

To read more information, click here .

Interface

An Interface serves as a framework within our application, functioning as a binding agreement. It specifies the syntax that classes must adhere to; thus, any class that implements an interface is required to provide implementations for all its members. While we cannot create an instance of an interface, it can be referenced through the object of the class that implements it.

The interface solely comprises the declarations of methods and fields, lacking any implementation. Consequently, it cannot be utilized for constructing anything. A class that inherits from an interface must implement all the members defined by that interface.

Upon compilation by the TypeScript compiler into JavaScript, the interface will be eliminated from the resulting JavaScript file. Therefore, its role is limited to assisting during the development phase only.

Interface Declaration

In TypeScript, an interface can be defined utilizing the interface keyword. The syntax outlined below illustrates how to declare an interface.

Example

interface interface_name {  
          // variables' declaration  
          // methods' declaration  
}

Use of Interface

We can use the interface for the following things:

  • Validating the specific structure of properties
  • Objects passed as parameters
  • Objects returned from functions.

To read more information, click here .

TypeScript class vs. TypeScript Interface

TypeScript Class TypeScript Interface
Introduction Classes are the fundamental entities used to create reusable components. It is a group of objects which have common properties. It can contain properties like fields, methods, constructors, etc. An Interface defines a structure which acts as a contract in our application. It contains only the declaration of the methods and fields, but not the implementation.
Usage It is used for object creation, encapsulation for fields, methods. It is used to create a structure for an entity.
Keyword We can create a class by using the class keyword. We can create an interface by using the interface keyword.
Compilation A class cannot disappear during the compilation of code. Interface completely disappeared during the compilation of code.
Real-Time Usage Design Pattern, Designing project Structure Implements of defined Architectures
Instantiation A class can be instantiated to create an object. An interface cannot be instantiated.
Methods The methods of a class are used to perform a specific action. The methods in an interface are purely abstract (the only declaration, not have a body).
Access Specifier The member of a class can be public, protected, or private. The members of an interface are always public.
Constructor A class can have a constructor. An interface cannot have a constructor.
Implement/Extend A class can extend only one class and can implement any number of the interface. An interface can extend more than one interfaces but cannot implement any interface.

Input Required

This code uses input(). Please provide values below: