ThisParameterType and OmitThisParameter
Concept and syntax
ThisParameterType<F> extracts an explicit this parameter, or unknown if absent. OmitThisParameter<F> removes that explicit receiver from the function type; it does not bind a runtime receiver. Use bind when a function needs an actual receiver.
Working example
Save each snippet separately as main.ts in a fresh directory with TypeScript 5.8.3. Compile with tsc --strict --target ES2022 --module commonjs --moduleDetection force --skipLibCheck --noEmitOnError main.ts and run accepted snippets with node main.js. Type aliases are erased; assignments and diagnostic checks demonstrate the type relations, not the printed output alone.
function add(this:{base:number},n:number){return this.base+n;} type Receiver=ThisParameterType<typeof add>; type Bound=OmitThisParameter<typeof add>;const r:Receiver={base:4}; const f:Bound=add.bind(r); console.log(f(3));Expected output:
7Common mistake
Intentionally invalid; compile separately and do not execute.
function add(this:{base:number},n:number){return this.base+n;} type Receiver=ThisParameterType<typeof add>; type Bound=OmitThisParameter<typeof add>;const r:Receiver={base:"4"};Expected diagnostic: TS2322. The explicit receiver requires a numeric base.
Practice and recap
Extract ThisParameterType from a function without explicit this.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
ThisParameterType and OmitThisParameter — knowledge check
Check the type transformation, trace output, diagnose a rejected example, and apply the concept to a new case.
Which statement about ThisParameterType and OmitThisParameter is accurate?
What is the complete stdout, represented as a JSON string (including newline characters)?
function add(this:{base:number},n:number){return this.base+n;} type Receiver=ThisParameterType<typeof add>; type Bound=OmitThisParameter<typeof add>;const r:Receiver={base:4}; const f:Bound=add.bind(r); console.log(f(3));Which diagnostic and explanation match the rejected example under the lesson settings?
function add(this:{base:number},n:number){return this.base+n;} type Receiver=ThisParameterType<typeof add>; type Bound=OmitThisParameter<typeof add>;const r:Receiver={base:"4"};If a function has no explicit this parameter, what does ThisParameterType return?