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.
type Methods<D> = { [name: string]: (this: D) => unknown } & ThisType<D>;
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
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:
- If the method declares an explicit
thisparameter, that type is used. - Otherwise, if the literal's contextual type contains
ThisType<T>,thisisT. - Otherwise, if the contextual type has no
ThisTypemarker,thisis the contextual type of the literal. - Otherwise (no contextual type),
thisis the literal's own type.
Usage notes
- The marker needs
noImplicitThis(part ofstrict). With it off,ThisTypeis ignored andthisisany. - 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, asdefinedoes above withbind. - For a single standalone function, an explicit
thisparameter (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.
Related
- OmitThisParameter
and ThisParameterType - Awaited
- Tutorial: this in functions, Utility types