conditional

ThisType<T>

A marker interface with no members that, when used in a contextual type, sets the `this` type inside method-like properties of an object literal. Requires `noImplicitThis` to be genuinely useful and is a compile-time only hint.

interface ThisType<T> { }

Usage patterns

Pattern Purpose
obj: T & ThisType<Ctx> Set `this` inside methods to Ctx
noImplicitThis Compiler flag needed to enforce this-typing
Vue-like mixin objects The canonical use case in typed component APIs
ThisType<Instance> Point `this` at the surrounding instance shape

Examples

type ObjectDescriptor<D, M> = { data?: D; methods?: M & ThisType<D & M> };
function make<D, M>(d: ObjectDescriptor<D, M>) { /* ... */ }

Classic pattern giving methods access to both data and other methods on `this`

const o = { x: 1, log(this: { x: number }) { return this.x; } };

Alternative: declare `this` per-method instead of using ThisType

declare const cfg: { data: { x: 1 } } & ThisType<{ x: number }>;

ThisType only takes effect through contextual typing, not free variables

type M = { greet(): string } & ThisType<{ name: string }>;

Inside greet, this is inferred as { name: string }

Gotcha

ThisType has no runtime or structural effect — it is a compile-time marker only. It works only through CONTEXTUAL typing (parameter positions), not as a free variable annotation.

Related utility types

← All TS utility types · TypeScript vs JavaScript · any vs unknown vs never