0Pricing
TypeScript Academy · Lesson

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

  1. Function overloads & call signatures
  2. this parameter typing; void, never
  3. Assertion functions & user-defined type guards
← Back to TypeScript Academy