Callback APIs
Converting a method type into a standalone callback that callers can invoke without a receiver argument.
TypeScript · Type Alias
OmitThisParameter<Type> removes an explicit this parameter from a function type. Functions without an explicit this remain unchanged, while generic information and all but the final overload are not preserved.
OmitThisParameter<Type> transforms a function type that declares an explicit this parameter into an equivalent callable type without that parameter. If the source function has no explicit this, it remains unchanged; when overloads or generics are present, only the final overload is retained and generic parameters are discarded.
OmitThisParameter<Type>| Name | Type | Description |
|---|---|---|
Type | function type | The function type whose explicit this parameter should be removed. |
A function type without the explicit this parameter, or Type unchanged when no such parameter exists.
this annotation passes through without a type change.type Method = (this: HTMLElement, event: Event) => void;
type Callback = OmitThisParameter<Method>;
const callback: Callback = (event) => {
console.log(event.type);
};
callback(new Event("click"));The resulting callback can be called without supplying a this argument.
Converting a method type into a standalone callback that callers can invoke without a receiver argument.
Deriving a public function type from an internal method declaration while hiding its explicit receiver.
Typing bound or detached methods where the receiver is supplied elsewhere.
It removes an explicitly declared this parameter from a function type. If no explicit this exists, the original type is returned unchanged.
Follow-up: What happens to an overloaded generic function? Only its last overload is propagated, and its generics are erased.
The resulting type is callable without a this argument, while ordinary parameters remain unchanged. This is useful when exposing a method as a standalone callback.
Follow-up: How would you preserve the receiver instead? Keep the original function type, or use a type that explicitly includes its this parameter.
It is a standard utility type with one type argument: OmitThisParameter<Type>. The argument is the function type from which the explicit receiver parameter should be removed.
Follow-up: What if the input is not declared with this? The utility leaves that type unchanged.
ThisParameterType<Type>Parameters<Type>ReturnType<Type>Omit<Type, Keys>