this parameter typing; void, never
Type the special this parameter, understand lexical this with arrows, and learn when to return void vs never.
Intro
Goal: Type the special this parameter, know how arrows capture this lexically, and choose between void (no value) and never (no return).
this parameter typing
Add a leading parameter named this to type the expected receiver. It is not a real argument and only applies to non-arrow functions.
function setTitle(this: { title: string }, t: string): void {
this.title = t;
}
const model = { title: "Old" };
setTitle.call(model, "New");
console.log(model.title);All lessons in this course
- Function overloads & call signatures
- this parameter typing; void, never
- Assertion functions & user-defined type guards