Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

TypeScript · Type Alias

ThisType

ThisType<T> is a marker for the contextual type of this inside object-literal methods. It is commonly used in factory descriptors so methods can access combined data and method members.

Since
2.3

How it works

ThisType<T> is a marker used in an object-literal contextual type to define the type of this inside its methods. It does not transform or return a new type; with noImplicitThis enabled, methods can see the combined data and method members.

Syntax

ThisType<Type>

Parameters

NameTypeDescription
TypeTypeThe type whose members become available through contextual this; in the documented factory pattern it is the combined data and methods type.

Returns

No transformed type; ThisType<T> acts as a contextual marker for the type of this.

Key points

  • ThisType changes contextual handling of this rather than mapping one type into another.
  • Place it in an object-literal contextual type to specify the method receiver.
  • A factory descriptor can make methods contextually see the combined data and method type.
  • The resulting receiver is an intersection containing both object state and available methods.
  • The pattern supports inference for methods while also deriving their contextual receiver type.

Common mistakes

  • The utility has no effect unless the compiler option noImplicitThis is enabled.
  • ThisType itself provides no members; its behavior comes from contextual typing rather than an ordinary structural transformation.

Example: Contextual this in object methods

type ObjectDescriptor<D, M> = {
  data?: D;
  methods?: M & ThisType<D & M>;
};

function makeObject<D, M>(desc: ObjectDescriptor<D, M>): D & M {
  const data = desc.data || {};
  const methods = desc.methods || {};
  return { ...data, ...methods } as D & M;
}

const obj = makeObject({
  data: { x: 0, y: 0 },
  methods: {
    moveBy(dx: number, dy: number) {
      this.x += dx;
      this.y += dy;
    },
  },
});

obj.moveBy(1, 2);

This descriptor lets methods use this as the intersection of the data and methods types.

When to use it

Data-plus-method object factories

Define object factories whose methods access properties supplied in a separate data object.

Mixin-style object construction

Model mixin-like APIs where methods operate on the assembled object.

Contextual method typing

Infer method declarations while simultaneously using their combined receiver type.

Interview questions

QWhat does ThisType<T> do?

ThisType<T> is a contextual marker for the type of this in object-literal methods. It does not produce a transformed type, and it requires noImplicitThis.

Follow-up: How can a factory expose both data and methods? Use a descriptor whose methods member is intersected with ThisType<D & M>, then return D & M.

QWhat is the effective this type in the makeObject pattern?

In the documented descriptor pattern, methods receive a contextual this type of D & M. That combines the data members with the method members.

Follow-up: Why is the methods property written as both an inference target and a source? It lets TypeScript infer M while using D & M to contextually type method bodies.

QWhen would you use ThisType?

ThisType is useful when an object literal contains methods that should access data supplied separately. A factory can use it to model mixin-like object construction without manually annotating every method's this value.

Follow-up: What compiler setting is required? Enable noImplicitThis; otherwise this inside these methods is not checked through this utility.

  • noImplicitThis
  • contextual typing
  • intersection types
  • ObjectDescriptor
  • makeObject

Source: Documentation - Utility Types - TypeScript. Checked 2026-10-15.