Define the shape of objects with interfaces and type aliases.
TypeScript lets you describe the exact shape an object must have using an interface or a type alias.
interface User {
name: string;
age: number;
}
const user: User = { name: "Alice", age: 30 };
An interface is like a contract. If you forget a field or use the wrong type TypeScript will tell you immediately.
Add ? to mark a field as optional:
interface Product {
id: number;
name: string;
description?: string; // optional
}
type does the same thing and is often interchangeable with interface:
type Point = {
x: number;
y: number;
};
Define and export a User interface with:
name: stringage: numberemail?: string (optional)Then create and export a user object that matches it, with at least name and age filled in.
// Define an exported interface named 'User' with: // name: string // age: number // email?: string (optional) // // Then export a 'user' object that satisfies the interface.