Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท Utility Types

TypeScript ThisType<T> utility type

Reference for ThisType<T> in TypeScript: a marker interface that sets the type of this inside the methods of an object literal, with noImplicitThis and examples.

ThisType<T> is a marker. It does not transform a type and it adds no members; the compiler simply looks for it in the contextual type of an object literal and, when it finds it, uses T as the type of this inside every method of that literal.

This is the tool behind "options object" APIs where methods are written in one place but run against a different object at runtime: Vue-style { data, methods } definitions, mixin builders and small DSLs. Without it, this inside such methods would be the object literal itself (or any). The marker only has an effect when noImplicitThis is on, which --strict enables.

TypeScript
type Methods<D> = { [name: string]: (this: D) => unknown } & ThisType<D>;

Try it

TypeScript Methods that see a different this

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.

TypeScript Without the marker this is the literal itself

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

TypeScript
interface ThisType<T> {}

ThisType<T> is declared as an empty interface in lib.es5.d.ts. It is not a mapped or conditional type, so there is nothing to compute.

Name Description
T The type to use for this inside methods of an object literal whose contextual type includes ThisType<T>.

The effective rule for the type of this in an object literal method, in order of priority:

  1. If the method declares an explicit this parameter, that type is used.
  2. Otherwise, if the literal's contextual type contains ThisType<T>, this is T.
  3. Otherwise, if the contextual type has no ThisType marker, this is the contextual type of the literal.
  4. Otherwise (no contextual type), this is the literal's own type.

Usage notes

  • The marker needs noImplicitThis (part of strict). With it off, ThisType is ignored and this is any.
  • It only works for object literals and only for methods, getters and function expressions written inside them. Arrow functions do not have their own this, so the marker is irrelevant to them.
  • Use an intersection to attach it: Methods & ThisType<D & Methods>. Putting it in a parameter or return type position is what makes it part of the contextual type.
  • It does not change runtime behavior. Your code must still bind or call the methods with a suitable this, as define does above with bind.
  • For a single standalone function, an explicit this parameter (function f(this: Point) {}) is simpler and checked at call sites. See ThisParameterType and this in functions.
  • Because ThisType<T> is an empty interface, using it where a value type is expected has no useful meaning; it is only for contextual typing.