Open In App

What is the difference between interface and type in TypeScript ?

Last Updated : 24 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In TypeScript, developers use both methods like Type and Interface to outline how objects should look. Each has its own special features that can come in handy depending on the situation. The choice between them ultimately depends on what the developer finds most suitable.

Type in TypeScript: The Type System in TypeScript portrays the different data types that are supported by the language. It is divided into three major sections that are Any Type, Built-In Type, and User-Defined Type. The Type System in TypeScript is responsible for checking the data type of any value taken before it can be provided as an input to the program. 

Example: 

javascript
type Geeks = {
  name: string;
  age: number;
};

type MoreGeeks = {
  email: string;
};

type CombinedGeeks = Geeks & MoreGeeks;

const gfg: CombinedGeeks = {
  name: "kgowda",
  age: 20,
  email: "kgowda@gmail.com"
};

console.log(gfg);

Output:

{"name": "kgowda","age": 20,"email": "kgowda@gmail.com"}

Interface in TypeScript: An Interface in TypeScript is a syntactical obligation that all entities must follow. It can only contain the declaration of the members and is responsible for defining the properties, methods, and events. In a way, it is responsible for defining a standard structure that the derived classes will have to follow. 

Example: 

javascript
// Creating a interface
interface Geeks {
  name: string;
  age: number
}

interface Geeks {
  email: string;
}

// Using the merged interface
const gfg: Geeks = {
  name: "kgowda",
  age: 20,
  email: "kgowda@gmail.com"
};

console.log(gfg);

Output

name: " kgowda", age: 20, email: " kgowda@gmail.com"

Difference between Type and Interface in TypeScript:

TypeInterface
It is a collection of data types.It is a form of syntax.
Types are more flexible.Interface is less flexible when compared to typescript types.
It uses the “type” keyword for creating new type.It uses the “interface” keyword for declaring an interface.
It supports the creation of a new name for a type.It provides a way to define the entities.
It has less comparatively less capabilities.It has comparatively more capabilities.
It does not support the use of an object.It supports the use of an object.
Multiple merged declarations cannot be used.Multiple merged declarations can be used.
Two types having the same name raise an exception.Two interfaces having the same name get merged.
It does not have implementation purposes.It has an implementation purpose.
It does not support the feature of implementing or extending union types in the class.It supports the implementation and extend feature in typescript.
One can create an intersection type by combining multiple types into a single type.One cannot create any new intersection interface.
It is also used for types such as primitives, unions, and tuples.It cannot be used with other types of declaration.

 Conclusion- Though both, Typescript types and interface differs in some features but both are almost similar, so one cannot stop using one over the other. The developer can choose which typescript he/she wants to use.


Similar Reads

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 interface or contract of a car. interface Audi { len
2 min read
TypeScript Interface vs Type Statements
In TypeScript, both "interface" and "type" statements are used to define the shape or structure of an object or a type. They provide a way to define custom types and enforce type checking in TypeScript code. However, there are some differences between the two. InterfaceInterfaces in TypeScript are used to define agreements for objects. They can con
3 min read
TypeScript Differences Between Type Aliases and Interfaces Type
TypeScript is an open-source programming language that adds static typing and other valuable features to JavaScript. TypeScript is also known as a super-set of JavaScript. Type Aliases and Interfaces Type are used to create custom types in TypeScript. In this article, we will learn about the Differences Between TypeScript Type Aliases and TypeScrip
4 min read
Difference between Tuple & Interface in TypeScript
Tuples and Interfaces in TypeScript are used for different purposes and they also have different characteristics. Some of the differences between tuples and interfaces are written below. Tuple Interface It is an ordered collection of elements with different data types. It is used to define the structure of an object in TypeScript The elements store
1 min read
Typescript Required<Type> Utility Type
In this article, we are going to learn about the Required<Type> in Typescript. TypeScript is a popular programming language used for building scalable and robust applications. One of the features of Typescript is Required<Type> which is a built-in utility type that allows you to create a new type that makes all properties of the origina
4 min read
Typescript Record<Keys, Type> Utility Type
In this article, we are going to learn about the Record<Keys, Type> in Typescript. TypeScript is a programming language that is a strict syntactical superset of JavaScript. It adds optional static typing and class-based object-oriented programming to JavaScript, one of the features is Record<Keys, Type> which is a built-in utility type
4 min read
Typescript Partial<Type> Utility Type
In this article, we are going to learn about the Partial<Type> that is available in the Typescript. One of the features in Typescript is Partial<Type> which is a built-in utility type. It helps to create a new type by making all properties of an existing type optional. Now we can pass properties of an object of a new type that is create
4 min read
Typescript Awaited<Type> Utility Type
In this article, we are going to learn about the Awaited<Type> in TypeScript. It is a programming language that is a strict syntactical superset of JavaScript. It adds optional static typing and class-based object-oriented programming to JavaScript, one of the features of the typescript is Awaited<Type> it is a utility type in TypeScrip
2 min read
TypeScript Omit<Type, Keys> Utility Type
In this article, we are going to learn about the Omit<Type, Keys> utility type in Typescript. TypeScript is a powerful language that provides many utility types to make development easier and more efficient. One of the Typescript features is Omit utility type. We can use it when, let's say there exists a type already but we want to create a n
4 min read
TypeScript Pick<Type, Keys> Utility Type
In this article, we are going to learn about the Pick<Type, Keys> utility type in TypeScript. TypeScript is a programming language that is a strict syntactical superset of JavaScript. It adds optional static typing and class-based object-oriented programming to JavaScript, Pick<Type, Keys> is one of the features of Typescript which is a
5 min read