Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Typing 'this' in Functions | Function Overloading and Context
TypeScript Functions and Parameters

bookTyping 'this' in Functions

When you use the this keyword in JavaScript, it refers to the context in which a function is called. In TypeScript, you can make your code safer and clearer by explicitly typing this in your function declarations. Typing this helps prevent bugs that occur when functions are called with an unexpected context, especially in object-oriented code or when passing functions as callbacks.

12345678910
function showId(this: { id: number }): void { console.log("ID:", this.id); } const user = { id: 101, showId }; user.showId(); // Output: ID: 101 const detachedShowId = user.showId; // detachedShowId(); // Error in TypeScript: The 'this' context of type 'void' is not assignable to method's 'this' of type '{ id: number }'.
copy

TypeScript enforces the type you specify for this in your function. If you call a function with an incorrect this context, TypeScript will show an error, helping you catch mistakes early. A common error is forgetting to bind the correct context or using a function as a callback where this is not set as expected. Remember, arrow functions do not have their own this, so you cannot type this in an arrow function declaration. Always ensure the this type matches the object that will call the function to avoid runtime issues.

question mark

Which statements about typing this in TypeScript functions are correct?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 3. Chapitre 2

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

Awesome!

Completion rate improved to 9.09

bookTyping 'this' in Functions

Glissez pour afficher le menu

When you use the this keyword in JavaScript, it refers to the context in which a function is called. In TypeScript, you can make your code safer and clearer by explicitly typing this in your function declarations. Typing this helps prevent bugs that occur when functions are called with an unexpected context, especially in object-oriented code or when passing functions as callbacks.

12345678910
function showId(this: { id: number }): void { console.log("ID:", this.id); } const user = { id: 101, showId }; user.showId(); // Output: ID: 101 const detachedShowId = user.showId; // detachedShowId(); // Error in TypeScript: The 'this' context of type 'void' is not assignable to method's 'this' of type '{ id: number }'.
copy

TypeScript enforces the type you specify for this in your function. If you call a function with an incorrect this context, TypeScript will show an error, helping you catch mistakes early. A common error is forgetting to bind the correct context or using a function as a callback where this is not set as expected. Remember, arrow functions do not have their own this, so you cannot type this in an arrow function declaration. Always ensure the this type matches the object that will call the function to avoid runtime issues.

question mark

Which statements about typing this in TypeScript functions are correct?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 3. Chapitre 2
some-alt