Open In App

TypeScript Interface vs Type Statements

Last Updated : 21 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Interface

Interfaces in TypeScript are used to define agreements for objects. They can contain properties, methods, and method signatures. Interfaces can be implemented by classes to enforce a specific structure and ensure that the class adheres to the contract defined by the interface. Interfaces support multiple inheritance, where a class can implement multiple interfaces.

Syntax

interface MyInterface {
    // Interface properties and methods
}

Example

In this example, we will see the implementation of the Interface.

 

Javascript




// Interface.ts
interface Person {
    name: string;
    age: number;
}
  
const person: Person = {
    name: "John Doe",
    age: 25,
};
  
console.log(person.name);
console.log(person.age);


Output

"John Doe"    // person.name
25                  // person.age

Type

Types in TypeScript are used to define custom types that can be used to represent a shape or structure of an object. Types can represent any valid type in TypeScript, including primitive types, union types, intersection types, etc. Types can be used for creating aliases of existing types, making complex types more readable and reusable.

Syntax

type MyType = {
    // Type properties and methods
}

Example

In this example, we will see the implementation of Type.

Javascript




// type.ts
type Point = {
    x: number;
     y: number;
};
  
const point: Point = {
    x: 10,
    y: 20,
};
  
console.log(point.x); 
console.log(point.y);


Output

10    // point.x
20    // point.y

Difference between Interface and type Statements

Type

Interface

Purpose

A type statement is an alias for an existing type. It allows to create custom types to give a more descriptive name to an existing type.

An interface is a set of rules that a class or object must adhere to. It specifies the methods and properties that the implementing class should have, without providing any implementation details.

Syntax

The syntax for a type statement varies depending on the programming language. It can involve keywords like “type,” “typedef,” “alias,” or specific syntax rules provided by the language.

An interface is declared using the “interface” keyword followed by the interface name and the list of method or property definitions.

Usage

Type statements are used to define custom types or to provide more descriptive names to existing types.

Interfaces are mainly used to achieve abstraction and provide a way to define common behavior across different classes.

Implementation

Type statements, in most cases, do not define behavior or implementation. They primarily define the characteristics or relationships of types.

Interfaces only define the structure and signatures of methods or properties, without providing any implementation details

Extensibility

Type statements, depending on the language, may or may not support multiple inheritance. They are primarily used to create new types or aliases for existing types.

Interfaces support multiple inheritance, meaning a class can implement multiple interfaces. This allows for greater flexibility and code reuse.

Polymorphism

They allow for polymorphism and can be implemented by multiple classes.

They can improve code readability and maintainability by making the intent of the code clearer.

With interface-like behavior, we can define objects or classes with specific method signatures or properties that other objects or classes should adhere to. By following the signatures, we can achieve interface-like behavior in JavaScript.

For type aliases, we can utilize TypeScript, a superset of JavaScript that introduces static typing features. TypeScript allows to create custom types using the type keyword, providing more descriptive names or combining existing types into new ones.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads