gpt-3.5-turbo-0125· OpenAI
WildChat-4.8M (AI2)
Create a class in typescript
- User
Create a class in typescript
- Assistant
Sure, here's an example of a simple class in TypeScript:
class Person { private name: string; private age: number; constructor(name: string, age: number) { this.name = name; this.age = age; } greet() { console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`); } } const john = new Person('John', 25); john.greet();In this example, we have a
Personclass with anameand anageproperty, along with a constructor method to initialize these properties. The class also has agreetmethod that logs a greeting message using thenameandageproperties of the instance. Finally, we create an instance of thePersonclass calledjohnand call thegreetmethod on it.