codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🔷

TypeScript

15 / 60 topics
15Interfaces in TypeScript16Type Aliases17Union Types18Intersection Types19Literal Types20Type Assertions
Tutorials/TypeScript/Interfaces in TypeScript
🔷TypeScript

Interfaces in TypeScript

Updated 2026-05-15
10 min read

Interfaces in TypeScript

Introduction

TypeScript, being a statically typed superset of JavaScript, provides developers with powerful tools to define types explicitly. One such tool is the interface. An interface allows you to define a structure that objects must follow, ensuring type safety and predictability in your code.

In this tutorial, we will explore how to define and use interfaces in TypeScript, focusing on their role in type checking and how they can be used to create robust and maintainable codebases.

Concept

An interface in TypeScript is a way to define the shape of an object. It specifies what properties and methods an object should have without specifying how those properties and methods are implemented. Interfaces are particularly useful when you want to enforce a contract that multiple classes or objects must adhere to.

Key Features of Interfaces

  1. Property Definitions: You can specify the types of properties that an interface should have.
  2. Method Signatures: You can define method signatures, indicating the parameters and return type of methods without implementing them.
  3. Extensibility: Interfaces can extend other interfaces, allowing for a flexible and hierarchical structure.

Defining an Interface

To define an interface in TypeScript, you use the interface keyword followed by the name of the interface and its body enclosed in curly braces {}. Here is a simple example:

TypeScript
1interface Person {
2firstName: string;
3lastName: string;
4age: number;
5}

In this example, the Person interface defines an object with three properties: firstName, lastName, and age. All these properties must be of specific types (string for names and number for age).

Implementing an Interface

Once you have defined an interface, you can use it to enforce that objects conform to its structure. Here’s how you can create an object that implements the Person interface:

TypeScript
1const person: Person = {
2firstName: "John",
3lastName: "Doe",
4age: 30
5};

In this example, the person object is explicitly typed as Person, meaning it must have all the properties defined in the Person interface.

Method Signatures

Interfaces can also include method signatures. Here’s an example where we define a method that logs a greeting message:

TypeScript
1interface Greeter {
2greet: () => void;
3}
4
5class Person implements Greeter {
6firstName: string;
7lastName: string;
8
9constructor(firstName: string, lastName: string) {
10 this.firstName = firstName;
11 this.lastName = lastName;
12}
13
14greet() {
15 console.log(`Hello, my name is ${this.firstName} ${this.lastName}`);
16}
17}

In this example, the Greeter interface defines a method greet that takes no parameters and returns nothing (void). The Person class implements the Greeter interface by providing an implementation for the greet method.

Examples

Example 1: Basic Interface Usage

Let’s look at a basic example where we define an interface for a rectangle and use it to create objects:

TypeScript
1interface Rectangle {
2width: number;
3height: number;
4}
5
6function calculateArea(rect: Rectangle): number {
7return rect.width * rect.height;
8}
9
10const myRectangle: Rectangle = { width: 10, height: 20 };
11console.log(calculateArea(myRectangle)); // Output: 200

In this example, the Rectangle interface defines two properties: width and height. The calculateArea function takes a parameter of type Rectangle and returns its area. We then create an object that conforms to the Rectangle interface and pass it to the calculateArea function.

Example 2: Extending Interfaces

Interfaces can extend other interfaces, allowing for more complex and flexible structures. Here’s an example:

TypeScript
1interface Animal {
2name: string;
3}
4
5interface Dog extends Animal {
6breed: string;
7}
8
9const myDog: Dog = { name: "Buddy", breed: "Golden Retriever" };
10console.log(myDog); // Output: { name: 'Buddy', breed: 'Golden Retriever' }

In this example, the Dog interface extends the Animal interface, adding an additional property breed. This means that any object of type Dog must have both name and breed properties.

What's Next?

Now that you have a solid understanding of interfaces in TypeScript, you might want to explore Type Aliases. Type aliases provide another way to define types and can be used for more complex scenarios where interfaces might not be as suitable. They are particularly useful when defining union or intersection types.

Stay tuned for the next tutorial on Type Aliases, where we will dive deeper into how they complement interfaces and enhance your TypeScript experience!

Info

Remember, interfaces are a powerful feature of TypeScript that help you enforce type safety and structure in your code. Use them wisely to create maintainable and scalable applications.


PreviousArrow FunctionsNext Type Aliases

Recommended Gear

Arrow FunctionsType Aliases