DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • React, Angular, and Vue.js: What’s the Technical Difference?
  • Subtitles: The Good, the Bad, and the Resource-Heavy
  • Integrating Jenkins With Playwright TypeScript: A Complete Guide
  • Efficiently Migrating From Jest to Vitest in a Next.js Project

Trending

  • Develop a Reverse Proxy With Caching in Go
  • Scalable System Design: Core Concepts for Building Reliable Software
  • Enhancing Security With ZTNA in Hybrid and Multi-Cloud Deployments
  • Stateless vs Stateful Stream Processing With Kafka Streams and Apache Flink
  1. DZone
  2. Coding
  3. JavaScript
  4. How to Define an Interface in TypeScript

How to Define an Interface in TypeScript

How to define an interface using the keyword interface, implement an interface in a class, extend an interface from another interface, and extend a class in an interface.

By 
Kunal Chowdhury user avatar
Kunal Chowdhury
·
Jul. 13, 18 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
201.3K Views

Join the DZone community and get the full member experience.

Join For Free

An interface in TypeScript contains only the declaration of the methods and properties, but not the implementation. It is the responsibility of the class that implements the interface by providing the implementation for all the members of the interface.

Today, in this TypeScript tutorial, we will learn how to work with interfaces in TypeScript. Continue reading to learn more.

Class Implementing Interfaces

Just like C# and Java, you can create the contract for classes by implementing an interface. An interface defines public properties and methods of a class. It does not have any private members and must not have any implementations of its members.

In TypeScript, you can define an interface by using the keyword interface as below. By default, all the members in an interface are public.

interface Person {
 fullName: string;

 toString();
}


Once the interface is defined, you can implement it in a class by following this convention: class [ClassName] implements [InterfaceName]. Let's create two classes named Employee and Customer implementing the Person interface:

class Employee implements Person {
 empID: string;
 fullName: string;

 constructor (eID: string, name: string) {
  this.empID = eID;
  this.fullName = name;
 }

 toString() {
  console.log(`EMP ID of ${fullName}: ${empID}`);
 }
}

class Customer implements Person {
 custID: string;
 fullName: string;

 constructor (cID: string, name: string) {
  this.custID = cID;
  this.fullName = name;
 }

 toString() {
  console.log(`Customer ID of ${fullName}: ${custID}`);
 }
}


Let's create the instance of the classes. As both the Employee and the Customer object is of type Person, let us create it this way:

letl  employee: Person = new Employee("E001", "Kunal");
let customer: Person = new Customer("C001", "");


Let's call the toString() method of both the instances and observe how it prints the person detail in the screen:

employee.toString(); // prints employee details
customer.toString(); // prints customer details


Interface Extending Another Interface

In TypeScript, you can also extend an interface from another interface. This allows you to copy the members of one interface into another. So, it gives a higher degree of flexibility by separating your interfaces into reusable components. For example, the TwoWheeler interface extends the Vehicle interface as below:

interface Vehicle {
}

interface TwoWheeler implements Vehicle {
}


In TypeScript, an interface can also extend multiple interfaces. For example, let's look at the following code where the TwoWheeler interface extends the Vehicle and Engine interfaces:

interface Vehicle {
}

interface Engine {
}

interface TwoWheeler extends Vehicle, Engine {
}


Interface Extending Classes

TypeScript allows you to extend an interface from a class type. In this case, the declaration of the members of the class gets inherited to the interface but not their implementations. This is as good as a class inheriting from an interface.

class Vehicle {
    constructor (public brand: string) { }

    getBrandName() {
        return brand;
    }
}

class Engine {
    constructor (public manufacturer: string) { }

    getManufacturerName() {
        return manufacturer;
    }
}

interface TwoWheeler extends Vehicle, Engine {
    getBrandName();
    getManufacturerName()
}


In other words, you can create an interface that extends a class and then it can be implemented in another class or interface.

Summary

So, today we have learned how to define an interface using the keyword interface, how to implement an interface in a class, how to extend an interface from another interface, and how to extend a class in an interface.

If you are from C# or Java background, an interface extending a class will be new to you in TypeScript. I hope you liked the tutorial. 

Interface (computing) TypeScript

Published at DZone with permission of Kunal Chowdhury, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • React, Angular, and Vue.js: What’s the Technical Difference?
  • Subtitles: The Good, the Bad, and the Resource-Heavy
  • Integrating Jenkins With Playwright TypeScript: A Complete Guide
  • Efficiently Migrating From Jest to Vitest in a Next.js Project

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!