Avatar ·

What are get and set (getters and setters) in TypeScript and how to use them correctly?

Hello everyone! I just started learning TypeScript after JavaScript, and I'm confused about getters and setters. In JS, I'm used to making object properties public and accessing them directly: `user.name = 'John'`. But in TypeScript, I see that many people use `get` and `set` to access properties. I don't quite understand why this is needed and how to implement it correctly. Here's an example of my code where I'm trying to make a User class with a getter and setter: ```typescript class User { private name: string; constructor(name: string) { this.name = name; } get name(): string { return this.name; } set name(value: string) { this.name = value; } } const user = new User('Alice'); console.log(user.name); ``` But TypeScript complains about this definition