JS Guide
HomeQuestionsSearchResources
Search

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionstypescript
PrevNext
typescript
junior
interfaces

What is the difference between interfaces and type aliases in TypeScript?

interfaces
type-aliases
types
objects
extends
Quick Answer

Both define object shapes, but interfaces can be extended and merged (declaration merging), while type aliases can represent unions, tuples, and primitives. Use interfaces for objects/classes and type aliases for unions or complex types.

Detailed Explanation

Interfaces:

  • Define object shapes
  • Can be extended with extends
  • Support declaration merging
  • Preferred for object types and public APIs
  • Can be implemented by classes

Type Aliases:

  • Can represent any type (primitives, unions, tuples)
  • Use intersection & to combine
  • Cannot be merged
  • More flexible for complex types
  • Better for unions and mapped types

When to Use:

  • Interface: Object shapes, class contracts, public APIs
  • Type: Unions, tuples, complex computed types
  • General rule: Start with interface, use type when needed

Code Examples

Interface basics
// Define object shape with interface
interface User {
  id: number;
  name: string;
  email: string;
  age?: number;  // Optional property
  readonly createdAt: Date;  // Cannot be modified after creation
}

// Using the interface
const user: User = {
  id: 1,
  name: 'Alice',
  email: 'alice@example.com',
  createdAt: new Date()
};

user.createdAt = new Date(); // Error: Cannot assign to 'createdAt' because it is read-only

// Extending interfaces
interface Employee extends User {
  department: string;
  salary: number;
}

const employee: Employee = {
  id: 1,
  name: 'Bob',
  email: 'bob@company.com',
  createdAt: new Date(),
  department: 'Engineering',
  salary: 80000
};

Resources

TypeScript - Interfaces

docs

TypeScript - Type Aliases

docs

Related Questions

What are union and intersection types in TypeScript?

junior
unions

What are generics in TypeScript and how do you use them?

mid
generics
Previous
What are the basic types in TypeScript?
Next
What are union and intersection types in TypeScript?