ThisParameterType<T>
Extracts the type of the this parameter from a function type, or unknown if the function has no this parameter.
Definition
type ThisParameterType<T> =
T extends (this: infer U, ...args: never) => any ? U : unknown;Examples
function toJSON(this: { name: string; age: number }) {
return JSON.stringify({ name: this.name, age: this.age });
}
type Context = ThisParameterType<typeof toJSON>;
// { name: string; age: number }
const obj: Context = { name: "Alice", age: 30 };
const result = toJSON.call(obj);interface Component {
element: HTMLElement;
render(this: Component): void;
}
type RenderContext = ThisParameterType<Component["render"]>;
// Component
function bindMethod<T extends (this: any, ...args: any[]) => any>(
fn: T,
context: ThisParameterType<T>,
): OmitThisParameter<T> {
return fn.bind(context);
}function add(a: number, b: number): number {
return a + b;
}
type AddThis = ThisParameterType<typeof add>;
// unknown (no this parameter declared)
type HasThis<T> = ThisParameterType<T> extends unknown ? false : true;Common Use Cases
- 1Extracting the expected this context from methods
- 2Building type-safe bind/call/apply wrappers
- 3Validating that methods are called with correct context
- 4Framework helpers that manage function binding
- 5Typing decorator patterns that modify this behavior
Understanding ThisParameterType<T>
ThisParameterType<T> extracts the type of the this parameter from a function type. In TypeScript, functions can declare an explicit this parameter as their first parameter—this doesn't appear at the call site but tells TypeScript what type this must be when the function is invoked.
This utility type uses infer within a conditional type to capture the this type. If the function doesn't have an explicit this parameter, the result is unknown.
The most common use case is building type-safe wrappers around Function.prototype.bind, call, and apply. When you need to bind a method to a specific context, ThisParameterType tells you what type that context must be. This prevents the common JavaScript bug of binding a method to the wrong object.
Frameworks that manage component lifecycles often need this type. Event handlers, for example, need to be called with the correct this context. By extracting the expected this type, the framework can validate bindings at compile time.
ThisParameterType pairs with OmitThisParameter. Where ThisParameterType tells you what the this context should be, OmitThisParameter gives you the function signature without the this parameter—useful for the bound version of the function.
In modern TypeScript, arrow functions and class fields have largely reduced the need for explicit this parameters. However, they remain important in certain patterns: prototype methods, mixin functions, and callback interfaces where the caller sets this. Understanding ThisParameterType helps you work with these patterns safely.
Related Types
OmitThisParameter<T>Removes the this parameter from a function type, producing a callable function signature without this context requirements.
Parameters<T>Extracts the parameter types of a function type T as a tuple, enabling you to reuse a function's argument types elsewhere.
ReturnType<T>Extracts the return type of a function type T, enabling you to reuse a function's output type without manual duplication.
infer keywordThe infer keyword declares a type variable within a conditional type's extends clause, extracting and capturing part of a type for use in the true branch.
More Object & Function Types
Explore TypeScript Types
Browse our complete reference of 30 TypeScript utility types with definitions, examples, and explanations.