Skip to content

Extending Interfaces and Types

Jorge Castro edited this page Nov 10, 2025 · 1 revision

Extending a type via intersections

type PersistenceFields = {
  createdAt: Date;
  updatedAt: Date;
};

type Person = {
  id: string;
  name: string;
  email: string;
};

type Student = {
  studentId: string;
  major: string;
} & Person & PersistenceFields;

Extending an interface

interface PersistenceFields {
  createdAt: Date;
  updatedAt: Date;
}

interface Person {
  id: string;
  name: string;
  email: string;
}

interface Student extends Person, PersistenceFields {
  studentId: string;
  major: string;
}

Using the new type

const student: Student = {
  studentId: "S12345",
  major: "Computer Science",
  id: "P67890",
  name: "Alice Johnson",
  email: "alice.johnson@example.com",
  createdAt: new Date(),
  updatedAt: new Date(),
};

Adding new fields to an existing interface

This works:

interface Student {
  courses?: string[];
}

student.courses = ["Math 101", "Physics 201"];

Adding new fields to an existing type alias

This won't work because type Students was already defined:

type Student = {
  courses?: string[];
}

student.courses = ["Math 101", "Physics 201"];

Clone this wiki locally