Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære 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

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 2

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Awesome!

Completion rate improved to 9.09

bookTyping 'this' in Functions

Stryg for at vise menuen

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

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 2
some-alt