Master TypeScript From Scratch

Learn TypeScript step by step with practical examples, interactive coding lessons, and real-world projects.

TypeScript Interfaces and Types

Learn how to define object structures, create reusable type definitions, combine types and work with enums and literal types in TypeScript.

Simple Explanation: Interfaces and type aliases describe the shape of data in TypeScript. They help developers write structured, reusable and type-safe code.

1. What are Interfaces and Types?

TypeScript allows us to define the expected structure and allowed values of data. Interfaces and type aliases are two important ways to describe types in an application.

For example, a Student object may need an ID, a name and an age. We can define these properties using an interface.

Example: Student Interface

interface Student {
    id: number;
    name: string;
    age: number;
}

const student1: Student = {
    id: 101,
    name: "Rahul",
    age: 22
};

console.log(student1.name);
console.log(student1.age);

Expected Output

Rahul
22

The Student interface describes the expected properties. TypeScript checks whether the object follows the defined structure.

2. Interfaces and Types Workflow Diagram

Follow this workflow to understand how type definitions help structure data in a TypeScript application.

📝
Define Structure

Declare an interface or a type alias.

→
📦
Create Data

Create an object or value using the defined type.

→
🔍
Type Checking

TypeScript checks the structure and compatible values.

→
✅
Use Data

Work with the validated values in your application.

Remember: TypeScript checks types during development and compilation. Interfaces and type aliases are generally removed from the generated JavaScript, so they do not perform runtime validation by themselves.

3. Interface vs Type Alias

Interfaces and type aliases both help define types, but they have different capabilities and common use cases.

📘

Interface

Commonly used to describe the structure of objects and class contracts.

interface Employee {
    id: number;
    name: string;
}
🏷️

Type Alias

Can name object shapes, union types, intersection types, primitives and other type expressions.

type EmployeeId = number;

type Status = "active" | "inactive";
Both interfaces and type aliases support object type definitions. Choose the one that best fits the type you need to describe.

4. Topics Covered in This Module

Learn each topic step by step with explanations, TypeScript code examples, expected outputs and visual diagrams.

📘

1. Interfaces

Define object structures, optional properties, methods and reusable contracts for objects and classes.

🏷️

2. Type Aliases

Create reusable names for object shapes, primitive types and other type expressions.

🔀

3. Union Types

Allow a value to match one of several specified types.

🔗

4. Intersection Types

Combine multiple type definitions into one type that satisfies all the included requirements.

🔢

5. Enums

Define a group of named constants for related values.

🎯

6. Literal Types

Restrict a value to specific string, number or boolean literal values.

5. Practical Example: Employee Details

Suppose an application stores employee details. An interface can define the required employee properties so different parts of the application follow the same structure.

Example

interface Employee {
    id: number;
    name: string;
    department: string;
}

const employee1: Employee = {
    id: 1,
    name: "Priya",
    department: "IT"
};

function displayEmployee(employee: Employee): void {
    console.log(employee.name);
    console.log(employee.department);
}

displayEmployee(employee1);

Expected Output

Priya
IT

How It Works

📋
Employee Interface

Describes the required properties.

→
👩‍💻
Employee Object

Stores the employee data according to the interface.

→
⚙️
Function

Receives an Employee value and displays its details.

6. Advantages of Interfaces and Types

Type Safety

Helps identify incompatible values and incorrectly structured objects.

Code Reusability

Reuse type definitions across functions, objects and classes.

Better Readability

Make the expected structure of data easier to understand.

Maintainability

Keep type definitions consistent as an application grows.

Summary

Interfaces and type aliases help define reusable types in TypeScript. Interfaces are commonly used for object structures, while type aliases can represent object types, unions, intersections and many other type expressions. Union types, intersection types, enums and literal types provide further ways to describe valid data.

Next, learn how to create interfaces, define properties and methods, and use interfaces with objects and classes.