Codectionary / Developer documentation / TypeScript

Typing a Function’s this Receiver

A special first parameter named this declares the expected receiver of an ordinary function. It is erased from emitted JavaScript and is not an argument callers pass positionally. This supports callback APIs and functions whose behavior depends on call, apply, or bind.

Syntax

function describe(this: { name: string }, prefix: string): string { /* ... */ }

Examples

Typing a Function’s this Receiver: working example

Run this standalone example after compiling with TypeScript 5.4+ in strict mode, targeting ES2022. Expected result: The program prints Hello Ada and Welcome Ada.

function describe(this: { name: string }, prefix: string): string {
  return `${prefix}${this.name}`;
}
const user = { name: "Ada" };
console.log(describe.call(user, "Hello "));
const greeting = describe.bind(user, "Welcome ");
console.log(greeting());
  1. Expected result: The program prints Hello Ada and Welcome Ada.
  2. The receiver must satisfy the named object shape under strict checking.
  3. The emitted function receives only prefix as an ordinary parameter.

Best practices

  • Enable strictBindCallApply, included by strict, for useful receiver and argument checking.
  • Use an arrow when lexical this is intended instead of declaring a dynamic receiver.

Runtime & versions

Compiled to JavaScript. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

TypeScript Handbook

Specifications & further reading