Objects, Interfaces and Type Aliases
interface and type describe the shape of an object. Add ? for optional properties and readonly for fixed ones. Interfaces can extend each other, and type aliases can name unions and more.
Example
interface User {
readonly id: number;
name: string;
email?: string;
}
interface Admin extends User { level: number }
const u: User = { id: 1, name: "Ada" };
const a: Admin = { id: 2, name: "Tunde", level: 3 };
console.log(u.name, a.level, u.email ?? "no email");Result
Ada 3 no email
Exercise
Mark a property optional.
interface A { email: string }
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.