0
TypeScript · Interfaces & Types
New technique
Interfaces describe shapes
Before you cast a blade, you carve the mould. An interface is the mould for your objects.
Most data in a real app is objects: a user, an order, a smith. An interface lists the properties such an object must have, and the type of each one.
Once you've named the shape, you can use it everywhere: as a variable type, a parameter, a return type. Forget a property or misspell one, and TypeScript points at it.
interface Smith {
name: string;
level: number;
}
const brunn: Smith = { name: "Brunn", level: 7 };
function promote(s: Smith): Smith {
return { ...s, level: s.level + 1 };
}- Interfaces exist only at compile time: no JavaScript is generated for them.
- Every property listed is required unless you say otherwise.