ThisParameterType<T>

Extracts the type of the this parameter of a function type T.

Since TS 3.3 Spec ↗

Syntax

ThisParameterType<Type>

Returns

type — The declared this parameter type, or unknown if the function has none.

Examples

function toHex(this: number) {
  return this.toString(16);
}

type T = ThisParameterType<typeof toHex>; // number
function bindCtx<F extends (this: any, ...a: any[]) => any>(
  fn: F,
  ctx: ThisParameterType<F>
) {
  return fn.bind(ctx);
}

Notes

Only meaningful for functions that declare an explicit `this` parameter. If no `this` parameter is declared the result is `unknown`. Use `OmitThisParameter` to strip it from the signature.

See also