← All languages
TypeScript construct of the day
Random

OmitThisParameter<T>

Remove the this parameter from a function type.

Description

The OmitThisParameter utility type removes the this parameter from a function type. If the function type has no explicit this parameter, the type is returned unchanged. This is useful when you need to pass a method as a standalone callback.

In TypeScript, functions can declare a this parameter as their first parameter to specify the type of this inside the function body. When extracting or passing these functions around, the this parameter type can cause compatibility issues that OmitThisParameter resolves.

This utility is commonly used with Function.prototype.bind, where the this context is pre-bound and the resulting function no longer needs the this parameter in its type signature.

Example

function greet(this: { name: string }, greeting: string): string {
  return `${greeting}, ${this.name}!`;
}

type GreetFn = typeof greet;
// (this: { name: string }, greeting: string) => string

type BoundGreet = OmitThisParameter<GreetFn>;
// (greeting: string) => string

const obj = { name: 'Alice', greet };
const bound: BoundGreet = obj.greet.bind(obj);

Reference