π¦ TypeScript Interfaces
An interface in TypeScript defines the shape of an object β the properties and their types β without creating actual implementation.
π Example Code: Defining & Using an Interface
Console output will appear here...
π‘ Interfaces are purely for type-checking at compile time β they donβt exist in the compiled JavaScript.
π§ Interface Syntax
- Use
interface
to declare a contract for object shapes
- Interfaces can be extended (inherit) from others
- They can be used to type variables, function parameters, or return values
π Extending Interfaces
interface Admin extends User {
role: string;
}
const admin1: Admin = {
name: "Jamie",
age: 30,
isActive: true,
role: "Moderator"
};
β‘ Interfaces are perfect for structuring data models and making your TypeScript code safer and scalable.