TypeScript Reference ยท Utility Types
TypeScript OmitThisParameter<T> utility type
Reference for TypeScript OmitThisParameter<T>: strip the this parameter from a function type, the type that bind() produces, and how to use it in wrappers.
OmitThisParameter<T> returns a function type identical to T except that the explicit this parameter is gone. If T has no this parameter, you simply get T back.
The classic use is describing a bound function: after fn.bind(obj), the this requirement is satisfied, so the result should no longer demand one. The built-in typing of bind relies on exactly this idea, and you can reuse it in your own helpers.
function area(this: { w: number; h: number }) { return this.w * this.h; } type Bound = OmitThisParameter<typeof area>; // () => number
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Syntax
type OmitThisParameter<T> = unknown extends ThisParameterType<T> ? T : T extends (...args: infer A) => infer R ? (...args: A) => R : T;
| Name | Description |
|---|---|
T |
A function type, possibly with a this parameter. |
Result: a function type with the same parameters and return type but no this. If T declares no this, the result is T itself.
Edge cases: for a non-function T, the result is T. For overloaded functions only the last signature survives when a this parameter is removed.
Usage notes
- Typed
bind. WithstrictBindCallApply(part ofstrict),bindon a function with athisparameter returns a function without it. The effect is the same asOmitThisParameter. - Only the type changes. It does not bind anything; you still call
bind, arrow-wrap or similar to supply the realthis. - When
thisis unknown: if the function never declaredthis, the first branch returnsTunchanged and nothing is rebuilt, so overloads and generics are kept. - Parameters survive: names, optional markers and rest elements carry over via
infer A. - Reading, not removing: use
ThisParameterType<T>to get the type ofthis. - Available since TypeScript 3.3.