TypeScript is an open-source programming language developed and maintained by Microsoft. It is a superset of JavaScript that adds optional static typing and class-based object-oriented programming. Since TypeScript extends JavaScript, it supports most object-oriented design principles, which facilitate the development of more robust and maintainable code. The following are some of the main object-oriented principles supported by TypeScript:
1. Encapsulation
Encapsulation is a core concept in object-oriented programming, involving combining an object's state (properties) and behavior (methods) into a single unit while hiding its internal implementation details from external access. In TypeScript, classes can be defined using the class keyword, with properties and methods declared within. Additionally, TypeScript provides access modifiers such as public, private, and protected to control the accessibility of properties and methods.
Example code:
typescriptclass Person { private name: string; constructor(name: string) { this.name = name; } public getName(): string { return this.name; } } let person = new Person("Alice"); console.log(person.getName()); // Output: Alice // console.log(person.name); // Error: Property 'name' is private and can only be accessed within class 'Person'
2. Inheritance
Inheritance enables new classes to inherit properties and methods from existing classes, promoting code reuse and establishing hierarchical class structures. TypeScript supports class inheritance using the extends keyword.
Example code:
typescriptclass Animal { move(distance: number = 0) { console.log(`Animal moved ${distance}m.`); } } class Dog extends Animal { bark() { console.log('Woof! Woof!'); } } const dog = new Dog(); dog.bark(); // Output: Woof! Woof! dog.move(10); // Output: Animal moved 10m.
3. Polymorphism
Polymorphism refers to the ability of objects from different classes to respond to the same message, meaning the same interface can be implemented differently by various instances. In TypeScript, this is typically achieved through inheritance and method overriding.
Example code:
typescriptclass Animal { makeSound() { console.log("Some generic sound!"); } } class Pig extends Animal { makeSound() { console.log("Oink! Oink!"); } } class Horse extends Animal { makeSound() { console.log("Neigh! Neigh!"); } } let animals: Animal[] = [new Pig(), new Horse()]; animals.forEach(animal => animal.makeSound()); // Output: Oink! Oink! // Output: Neigh! Neigh!
4. Abstraction
Abstraction involves defining a class's structure without fully implementing it, allowing other classes to provide concrete implementations. In TypeScript, the abstract keyword is used to define abstract classes and abstract methods. Abstract classes cannot be instantiated and are typically used as base classes for other classes.
Example code:
typescriptabstract class Animal { abstract makeSound(): void; move(): void { console.log("roaming the earth..."); } } class Dog extends Animal { makeSound() { console.log("Woof! Woof!"); } } let animal = new Dog(); animal.makeSound(); // Output: Woof! Woof! animal.move(); // Output: roaming the earth...
Through these object-oriented principles and features, TypeScript enhances code modularity, reusability, and maintainability, making it easier to develop large-scale applications.