interface
Try it yourself →Defines the shape (fields) of an object.
Syntax
interface Name { field: type; }Description
Determines which fields and types an object must have. Objects that do not conform to this shape will throw an ERROR.
Example
interface Person {
name: string;
age: number;
}
const k: Person = { name: "Alex", age: 36 };
console.log(k.name);Expected output
Alex
When to use?
Defining the shape of structured data like users, products, API responses.
Common mistakes
- ⚠️ Leaving a required field out throws an ERROR; use area?: type for optional ones.