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
ThisParameterType<T>
Extracts the explicit `this` parameter type from a function T, or `unknown` if none is declared. Useful for reflecting on method-style callbacks that require a specific caller.
OmitThisParameter<T>
Returns a function type identical to T but without its explicit `this` parameter. Commonly paired with `Function.prototype.bind` to model the bound function's signature.
InstanceType<T>
Given a constructor type T (typically obtained via `typeof ClassName`), returns the type of an instance produced by `new T()`. Useful for factory functions and generic wrappers over classes.
Record<K, V>
Creates an object type whose keys come from the union K and whose values all have type V. Useful for lookup tables, dictionaries, and enum-keyed maps.
Awaited<T>
Models the type produced by awaiting T, recursively unwrapping nested Promises (and other thenables). Introduced in TS 4.5 and used internally by the `await` expression's type.
← All TS utility types · TypeScript vs JavaScript · any vs unknown vs never