TypeScript Interfaces
An interface names the shape an object must have: which properties exist and what type each one is. TypeScript checks this structurally, so any object with the right properties fits, with no need to declare that it implements the interface.
TypeScript
interface Student {
name: string;
score: number;
email?: string;
}
function describe(student: Student): string {
const contact = student.email ?? "no email";
return `${student.name}: ${student.score} (${contact})`;
}
const ada: Student = { name: "Ada", score: 91 };
const grace: Student = { name: "Grace", score: 88, email: "grace@example.com" };
console.log(describe(ada));
console.log(describe(grace));