function-types

ThisParameterType<T>

Extracts the explicit `this` parameter type from a function T, or `unknown` if none is declared. Useful for reflecting on method-style callbacks that require a specific caller.

type ThisParameterType<T> = T extends (this: infer U, ...args: never) => any ? U : unknown

Usage patterns

Pattern Purpose
ThisParameterType<typeof fn> Read a function's this parameter
OmitThisParameter<T> Companion that strips the this parameter
function fn(this: X, ...) The declaration form that supplies a this parameter
fn.bind(x) Runtime companion for binding this

Examples

function toHex(this: number) { return this.toString(16); }
type T = ThisParameterType<typeof toHex>;

Result: number

function plain(x: number) { return x; }
type T = ThisParameterType<typeof plain>;

Result: unknown — no this parameter is declared

type M = ThisParameterType<Array<number>['map']>;

Reflects the this-binding of a built-in method

type X = ThisParameterType<{ f(this: Date): void }['f']>;

Result: Date

Gotcha

Only reads the `this: T` pseudo-parameter — arrow functions cannot declare one, so ThisParameterType always returns unknown for them. Enable `noImplicitThis` to make this parameter meaningful.

Related utility types

← All TS utility types · TypeScript vs JavaScript · any vs unknown vs never