interface
class
Defines the shape of an object. Supports optional properties (?), readonly, and extension (extends).
Syntax
interface Name { property: type; method(): returnType; }
Example
javascript
interface User {
id: number;
name: string;
email: string;
age?: number; // optional
readonly createdAt: Date; // immutable
}
interface Admin extends User {
role: 'admin' | 'superadmin';
permissions: string[];
}
const user: User = {
id: 1,
name: 'Alice',
email: 'alice@example.com',
createdAt: new Date(),
};