-
Notifications
You must be signed in to change notification settings - Fork 0
Extending Interfaces and Types
Jorge Castro edited this page Nov 10, 2025
·
1 revision
type PersistenceFields = {
createdAt: Date;
updatedAt: Date;
};
type Person = {
id: string;
name: string;
email: string;
};
type Student = {
studentId: string;
major: string;
} & Person & PersistenceFields;interface PersistenceFields {
createdAt: Date;
updatedAt: Date;
}
interface Person {
id: string;
name: string;
email: string;
}
interface Student extends Person, PersistenceFields {
studentId: string;
major: string;
}const student: Student = {
studentId: "S12345",
major: "Computer Science",
id: "P67890",
name: "Alice Johnson",
email: "alice.johnson@example.com",
createdAt: new Date(),
updatedAt: new Date(),
};This works:
interface Student {
courses?: string[];
}
student.courses = ["Math 101", "Physics 201"];This won't work because type Students was already defined:
type Student = {
courses?: string[];
}
student.courses = ["Math 101", "Physics 201"];- Sync-Obsidian-with-GitHub-on-iOS
- Obsidian-community-plugins
- Optimizing-Novel-Word-Count-Plugin-for-Git-Users
- What is TypeScript, and why you should use it
- Quick setup
- Inference and type annotations
- Fundamental Types
- Object Types
- Union Types
- Type Aliases and Interfaces
- Type Assertions
- Literal Types
- null and undefined
- Narrowing
- Creating Types from Types
- Common Built‐in and Utility Types
- tsc CLI ‐ (The Compiler)
- Avoid using Object or {}
- Ignoring TypeScript errors
- TypeScript resources