In TypeScript, an Interface is a way to define a contract or a blueprint for a type.
It specifies the properties and methods that a particular object must have to be considered an instance of that type.
Interfaces are a powerful tool for achieving type-checking and enforcing structure in your code.
Interfaces are commonly used to define the shape of an object.
interface Employee { name: string; deptNo: number; deptName: string; } let employee: Employee = { name: "Alice", deptNo: 007, deptName: 'HR' };
Note: The `Employee` interface defines a structure with "name", "deptNo", and "deptName" properties. The `employee` object must conform to this structure.
interface Person { name: string; age: number; govtId?: number; } let person: Person = { name: "Alice", age: 18, };
Properties in an interface can be made optional by appending "?" to their names.
Note: "govtId" is an optional property, and it's not required to be present in the "person" object.
interface Person { readonly name: string; readonly govtId: number; } let person: Person = { name: "Alice", govtId: 123654 }; // person.govtId = 334422; // Error: Cannot assign to 'govtId' because it is a read-only property.
interface Calculator { (num1: number, num2: number): number; } let multiple: Calculator = (num1, num2) => num1 * num2; console.log(multiple(10, 5)) // 50; interface Calculator { (radius: number): number; } let circleArea: Calculator = (radius) => 3.14159 * radius * radius; console.log(circleArea(100)) // 31415.9;
Properties can be marked as read-only, indicating that they can only be assigned a value when the object is created.
interface Dictionary { [key: string]: string; } let myDictionary: Dictionary = { "apple": "APPLE", "may": "MAY", "1": "one" };
Interfaces can define the signature of a function.
Note: The `calculator` interface represents a function that takes two numbers and returns a number.
interface Education { eduInfo: string; } interface Person extends Education { name: string; } let person: Person = { name: "Alice", eduInfo: "Post Graduate" };
interface Vehicle { price: number; capacity: number; airbags: number; makeHorn(): void; isBulletProof(): boolean; } class Car implements Vehicle { price: number; airbags: number; capacity: number; constructor(price: number, airbags: number, capacity: number) { this.price = price; this.airbags = airbags; this.capacity = capacity; } makeHorn(): void { console.log("pee pee!"); } isBulletProof(): boolean { if(this.price > 100000) { return true; } return false; } } let vehicle: Vehicle = new Car(120000, 5, 2); console.log(vehicle.price); console.log(vehicle.isBulletProof()); console.log(vehicle.makeHorn()); console.log(vehicle.airbags); console.log(vehicle.capacity);
Interfaces can describe types that have index signatures.
Note: This allows us to create objects with dynamic keys and values of a specific type.
Interfaces can extend other interfaces to create a new interface that combines their members.
Interfaces can be used to define the contract that a class must implement.