typescript example
TypeScript interface example
Describe a user object with an interface, then print a formatted line. No signup required.
Interfaces name a shape. Once User exists, greet can accept that shape instead of a loose object.
Add an optional field or a second user and watch how the type guides the edit — then Run to confirm Output.
interface User {
name: string;
score: number;
}
function greet(user: User): string {
return `${user.name} has ${user.score} points`;
}
const player: User = { name: "Asha", score: 12 };
console.log(greet(player));