When it comes to the object-oriented terms that TypeScript supports, the correct answer is that TypeScript supports all of the terms mentioned - Interfaces, Classes, and Modules. These object-oriented programming concepts are foundational to TypeScript and empower developers with better ways to write and manage code.
In TypeScript, an interface is a powerful way to define custom types. It allows you to define the shape of an object, ensuring it matches specific criteria. Interfaces are used to describe user-defined types, and they don't become part of the compiled JavaScript code.
A simple example of using interface in TypeScript is as follows:
interface Student {
id: number;
name: string;
}
Classes are a core aspect of object-oriented programming and TypeScript provides full support for them. TypeScript classes include features like constructors, methods, access modifiers (private, protected, and public), and property shorthand.
Consider the following TypeScript class example:
class Employee {
id: number;
name: string;
constructor(id: number, name: string) {
this.id = id;
this.name = name;
}
describe(): void {
console.log(`Employee: ${this.name}`);
}
}
Modules are TypeScript's way of handling code organization and reuse. They help in encapsulating related code into a single unit of code, which can be exported and then imported for use in other modules.
An example of a module in TypeScript:
// math.ts
export function add(x: number, y: number): number {
return x + y;
}
// app.ts
import { add } from './math';
console.log(add(2, 3)); // Outputs: 5
In conclusion, TypeScript has strong support for object-oriented programming concepts like interfaces, classes, and modules. These features not only provide code structure but also improve its maintainability and reusability, thereby making TypeScript a powerful tool for building scalable applications.