ThisParameterType, OmitThisParameter, and ThisType

ThisParameterType, OmitThisParameter, and ThisType: this parameters versus the ThisType marker, which is not a global this binding.

ThisParameterType and OmitThisParameter read or remove a function this parameter. They are compile-time aliases in the standard typings, added in TypeScript 3.3. They do not call bind and they do not change the emitted JavaScript of an existing function.

ThisType is a different tool: an empty marker interface used for contextual this in object literals, typically through an intersection with the method object. Official documentation notes that --noImplicitThis must be enabled to use it. That flag is included in tsc --strict, which is how these examples are checked. ThisType does not rebind this for a class, a function declaration, or the rest of the file, and it is not a runtime value.

Examples were checked with TypeScript 5.9.3 using tsc --strict. See the TypeScript handbook for the complete utility-type catalog.

TypeScript reference overview →

API index

APISummary
ThisParameterType, OmitThisParameterRead or remove a function this parameter. These aliases do not bind this at runtime.
ThisTypeA compile-time marker for contextual this on object literals. It is not a global this binding and not a runtime value.

ThisParameterType and OmitThisParameter

For a function whose first declared parameter is this with an object type that has a name field, ThisParameterType is that object type. If a function has no this parameter, ThisParameterType is unknown, so almost any value is assignable to it. OmitThisParameter produces a function type without that this parameter, which is the usual type of the function after bind. The alias only changes the type: assigning an unbound function to OmitThisParameter may be allowed, but calling it without a this value is still a runtime mistake. The illegal free call in the example is written as false && greet("!") so the type error is checked and the call is not executed.

ThisParameterType, OmitThisParameter

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

type OmitThisParameter<T> = unknown extends ThisParameterType<T> ? T : T extends (...args: infer A) => infer R ? (...args: A) => R : T;

Read or remove a function this parameter. These aliases do not bind this at runtime.

Type parameterDescription
TA function type, usually typeof fn. If T is not a function with a this parameter, ThisParameterType is unknown and OmitThisParameter returns T.

Produces: The this parameter type, or a function type with that parameter removed. Neither alias is a runtime function.

Type errors

  • type error — Calling a function that still has a required this parameter as a free function is a type error under noImplicitThis, which is included in --strict: the this context is void.
  • The signatures are from TypeScript 5.9.3 lib.es5.d.ts, formatted for reading. These aliases arrived in TypeScript 3.3.
  • Parameters and ReturnType, which ignore this and use the last overload, stay on the Parameters page.
typescript
function greet(this: { name: string }, punctuation: string) {
  return this.name + punctuation;
}

const fn: OmitThisParameter<typeof greet> = greet.bind({ name: "Ada" });
console.log(fn("!"));
console.log(greet.call({ name: "Ada" }, "?"));

type ThisArg = ThisParameterType<typeof greet>;
const ctx: ThisArg = { name: "Ada" };
console.log(greet.call(ctx, "."));

function plain(n: number) {
  return n.toString(16);
}
const unknownThis: ThisParameterType<typeof plain> = "no this parameter, so unknown";

// @ts-expect-error calling greet as a free function has a void this context
false && greet("!");

Expected output

Ada!
Ada?
Ada.

Related: Parameters and ReturnType, ThisType

Official documentation →

ThisType

ThisType does not transform its type argument into another object type. It marks a contextual this type for methods in an object literal, usually through an intersection. The helper below copies data and methods onto one object so that, at runtime, this in inc and value is that combined object. Without the marker, this in the methods object does not have count, and tsc reports a type error under noImplicitThis.

A class method still uses the class instance as this. ThisType does not replace that, and it does not set the this type of other functions in the file. Official typings declare ThisType as an empty interface, a type-only marker, not a constructor or a value.

ThisType

interface ThisType<T> {}

A compile-time marker for contextual this on object literals. It is not a global this binding and not a runtime value.

Type parameterDescription
TThe type of this that methods in the marked object literal should see. Often the intersection of the data and method types.

Produces: ThisType does not produce a transformed type. It is an empty interface used as a marker.

Type errors

  • type error — Using ThisType as a value is an error: it only refers to a type. Accessing a data field on this without the marker, when this is just the methods object, is also an error under noImplicitThis.
  • Official documentation requires --noImplicitThis. These examples use tsc --strict, which includes that flag. The project tsconfig also enables strict.
typescript
function makeObject<D, M>(desc: { data: D; methods: M & ThisType<D & M> }): D & M {
  return { ...desc.data, ...desc.methods };
}

const counter = makeObject({
  data: { count: 0 },
  methods: {
    inc() {
      this.count++;
    },
    value() {
      return this.count;
    },
  },
});
counter.inc();
console.log(counter.value());

function makePlain<D, M>(desc: { data: D; methods: M }): D & M {
  return { ...desc.data, ...desc.methods };
}
const plain = makePlain({
  data: { count: 0 },
  methods: {
    inc() {
      // @ts-expect-error without ThisType, this is the methods object
      this.count++;
    },
  },
});

class Other {
  n = 1;
  inc() {
    this.n++;
  }
}
const other = new Other();
other.inc();
console.log(other.n);

// @ts-expect-error ThisType is only a type, not a runtime value
type NotAValue = typeof ThisType;

Expected output

1
2

Related: ThisParameterType

Official documentation →