Interfaces & Objects
Interfaces define the shape of objects — what properties they must have and their types.
25 min•By Priygop Team•Last updated: Feb 2026
TypeScript Interfaces
An interface defines the structure (shape) of an object. It specifies what properties an object must have and their types. If an object doesn't match the interface, TypeScript shows an error. Interfaces are purely a TypeScript feature — they don't exist in compiled JavaScript.
interface Features
- interface Name { } — Defines the shape of an object
- Required properties — Must be present in every object
- Optional properties — Use ? to make them optional: age?: number
- readonly properties — Cannot be changed after creation
- Interfaces can extend other interfaces
- Used for function parameters to ensure correct shape
Interfaces Example
Example
// Define an interface
interface User {
id: number;
name: string;
email: string;
age?: number; // Optional property
readonly role: string; // Cannot be changed
}
// Create objects that match the interface
const user1: User = {
id: 1,
name: "Alice",
email: "alice@example.com",
age: 25,
role: "admin"
};
const user2: User = {
id: 2,
name: "Bob",
email: "bob@example.com",
// age is optional — can be omitted
role: "user"
};
// Function that accepts a User interface
function greetUser(user: User): string {
return `Hello, ${user.name}! Role: ${user.role}`;
}
console.log(greetUser(user1));
console.log(greetUser(user2));Try It Yourself: Interfaces
Try It Yourself: InterfacesJavaScript
JavaScript Editor
✓ ValidTab = 2 spaces
JavaScript|34 lines|1109 chars|✓ Valid syntax
UTF-8