TypeScript
this in TS
TypeScript types this to avoid context loss in callbacks and methods.
In classes
class Counter {
count = 0;
increment() {
this.count++;
}
}In callbacks
class Button {
label = "Click";
handleClick() {
console.log(this.label);
}
}
const b = new Button();
setTimeout(() => b.handleClick(), 1000);Explicit this typing
function greet(this: { name: string }) {
console.log(`Hello, ${this.name}`);
}