class Person { name: string; age: number; constructor(name: string, age: number) { this.name = name; this.age = age; } greet(): void { console.log(`Hello, my name is ${this.name} and I am ${this.age} years old`); } } const john = new Person("John", 30); john.greet();
const EventEmitter = require('events'); class MyEmitter extends EventEmitter {} const myEmitter = new MyEmitter(); myEmitter.on('myEvent', () => { console.log('Event emitted'); }); myEmitter.emit('myEvent');In the first example, we create a class called Person and instantiate it with a name and an age. We then define a greet method that logs a message to the console. This code is written in TypeScript. In the second example, we create a new class called MyEmitter that extends the built-in EventEmitter class in Node.js. We then create a new instance of that class and define an event listener for the 'myEvent' event. Finally, we emit the 'myEvent' event, which triggers the listener to log a message to the console. This code is written in JavaScript and requires the 'events' package/library. In conclusion, TypeScript and EventEmitter are two different tools used for different purposes. TypeScript is used to write code with optional static typechecking, while EventEmitter is used to emit and handle events in Node.js and frontend frameworks. The package/library needed to use EventEmitter is 'events'.