TypeScript Interfaces
TypeScript: Interfaces
Interfaces describe the structure of an object — like a blueprint for what it should look like.
What Is an Interface?
Imagine you're working with objects that follow a specific pattern.
Instead of repeating that pattern again and again, you can define it once using an interface.
It tells TypeScript:
“Any object using this interface must include these properties and types.”
How to Write One
Here’s a basic example of an interface:
interface User {
name: string;
age: number;
isAdmin: boolean;
}
Now you can use it like this:
TypeScript will make sure all required properties are there — and have the correct types.
Why Use Interfaces?
- Keeps code consistent
- Makes complex data easier to understand
- Great for organizing function parameters, API responses, and more
- Helps with auto-completion in code editors
Optional Properties
Sometimes not every property is required. You can mark them as optional with a ?:
interface Product {
title: string;
price: number;
description?: string;
}
Now description is not mandatory.
Reusable & Extendable
Interfaces can be extended — you can build on top of them:
interface Employee extends User {
department: string;
}
This creates a new type that includes everything from User plus new properties.
Interfaces in Functions
You can also use interfaces to define what kind of object a function expects:
function showUser(user: User) {
Console.log(`${user.name} is ${user.age} years old.`);
}
Prefer Learning by Watching?
Watch these YouTube tutorials to understand TYPESCRIPT Tutorial visually:
What You'll Learn:
- 📌 TypeScript Interfaces
- 📌 Typescript tutorial for beginners #9 Interface