What is an interface in TypeScript?

Beginner

Answer

An interface defines the structure of an object, specifying what properties and methods it should have.

interface User {
  id: number;
  name: string;
  email: string;
  isActive?: boolean; // Optional property
  readonly createdAt: Date; // Read-only property
}

const user: User = {
  id: 1,
  name: "John Doe",
  email: "john@example.com",
  createdAt: new Date()
};