Aarav Sharma
Right-hand opener · India
Same fields, different player. That’s an interface.
Every player has a card. Every card has the same fields — name, role, jersey, country, strike rate. Aarav Sharma's card and Owen Pretorius' card hold different values, but they have the same shape.
TypeScript calls that shape an interface. You define it once: “a Player must have these fields, with these types.” Then every object you label as a Player has to satisfy that contract. Miss a field, change a type, and the compiler will catch it.
Tap a different player below to swap cards. Then try the × on any field — TypeScript will not let you drop it.
Right-hand opener · India
Define the shape once. Every Player object then has to satisfy it — same fields, same types. Different values are fine; missing fields are not.
interface Player { name: string; role: string; jersey: number; strikeRate: number; isCaptain: boolean; } const player: Player = { name: 'Aarav Sharma', role: 'Right-hand opener', jersey: 7, strikeRate: 165, isCaptain: true, };
You just used the Player interface to look at five very different players. Same five fields each time — name, role, jersey, strike rate, captain flag — but the values changed completely. That's what interfaces are for: the shape is a contract, the contents are yours to fill.
When you remove a required field, the compiler stops you cold. When you give a field the wrong type — jersey as a string, isCaptain as a number — it stops you the same way. Interfaces are how TypeScript ensures every Player you build is actually a Player.
This is one of 9 paid lessons. Lessons 1 and 9 are free previews if you want to sample first — otherwise the full course unlocks in one click.