Loading

TypeScript

What is the difference between interface and type in TypeScript ?. The Complete TypeScript Developer Course 2023 [Videos].

Both the methods Type and the Interface are used to describe the structure of the objects in TypeScript. But holds some specific features that will be helpful according to the situation, choosing between them and totally depends on the developer.

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 TypeBuilt-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:

// Creating a type
type Geeks {
  name: string;
  age: number
}
  
type Geeks {
  email: string;
}
  
// Using the merged type
const gfg: Geeks = {
  name: " kgowda",
  age: 20,
  email: " kgowda@gmail.com"
};
  
console.log(gfg);

Output:

"Duplicate identifier "Geeks"" error.

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 propertiesmethods, and events. In a way, it is responsible for defining a standard structure that the derived classes will have to follow.

Example:

// 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.
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.

See All

Comments (165 Comments)

Submit Your Comment

See All Posts

Related Posts

TypeScript / Blog

Difference between TypeScript and JavaScript

When JavaScript was developed the JavaScript development team introduced JavaScript as a client-side programming language. But when people were using JavaScript then developers get to know that JavaScript can be used as a server-side programming language also. But When JavaScript was growing then the code of JavaScript became complex and heavy.
19-jan-2022 /33 /165

TypeScript / Blog

What is the difference between interface and type in TypeScript ?

Both the methods Type and the Interface are used to describe the structure of the objects in TypeScript. But holds some specific features that will be helpful according to the situation, choosing between them and totally depends on the developer.
/33 /165

TypeScript / Blog

What is the Function type in TypeScript ?

TypeScript is a JavaScript-based programming language with a typed syntax. It provides improved tools of any size. It adds extra syntax to JavaScript. This helps in facilitating a stronger interaction between you and your editor. It also helps in catching the mistakes well in advance.
19-jan-2022 /33 /165