JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

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

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionstypescript
PrevNext

Learn the concept

Interfaces vs Type Aliases

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: The TypeScript handbook suggests starting with interface. Others recommend type by default and using interface only when declaration merging or extends is needed. Most importantly, be consistent within your codebase

Code Examples

Interface basicsTypeScript
// 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
};

Real-World Applications

Use Cases

Component Props Definition

Defining React component props with interfaces for extendability and declaration merging

API Layer Contracts

Using interfaces for service contracts that different implementations can satisfy

Plugin System Types

Using declaration merging to allow plugins to extend core type definitions

Mini Projects

Shape Calculator with Interfaces

beginner

Define shape interfaces (Circle, Rectangle, Triangle) implementing a common Measurable interface

Extensible Theme System

intermediate

Build a theme type system using declaration merging to allow plugins to add custom theme properties

Industry Examples

Express.js

Uses declaration merging to let middleware extend Request/Response interfaces

Next.js

Uses module augmentation for extending page props and API route types

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?
PrevNext