TypeScript provides several basic types: string, number, boolean, array, tuple, enum, any, void, null, undefined.
Interfaces define the shape of an object.
TypeScript provides several basic types: string, number, boolean, array, tuple, enum, any, void, null, undefined.
Interfaces define the shape of an object.
interface User { name: string; age: number; email?: string; } const user: User = { name: "John", age: 30 };
interface User { id: number; name: string; email: string; age?: number; } const user: User = { id: 1, name: "Alice", email: "alice@example.com" }; function showUser(u: User): void { console.log(`${u.name} (${u.email})`); } showUser(user);
Create an interface for a Product with name and price.
interface Product {
name: string;
price: number;
}
Which keyword defines an interface in TypeScript?
The "interface" keyword defines the shape of an object in TypeScript.
Both define types, but interfaces can be extended and implemented. Types are more flexible for unions and intersections.