TypeScript Utility Types
ThisType<T> — contextual receiver marker
ThisType is a compiler-recognized marker interface used in contextual object-literal typing. It gives methods a contextual this type when noImplicitThis is enabled (including under strict). It is not a transformation that returns T, and it does not add runtime properties or bind methods. Ensure the actual receiver satisfies the contract. Example output: "3". Verified with strict TypeScript 5.8.3, ES2022 and Node.js 22.16.0. This utility affects static typing; no runtime validation or transformation is supplied by the type itself.
Formal Syntax & Structure
typescript syntaxtypescript
ThisType<T>Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| T | type parameter | Contextual receiver type for object-literal methods. |
Working Example
Example Snippettypescript
type Counter = { count: number; add(n: number): void };const counter: Counter & ThisType<Counter> = { count: 1, add(n) { this.count += n; }};counter.add(2);console.log(counter.count);