These utility types help you work with the special this parameter in functions. They let you get or remove the type of this to make your code safer and clearer.
ThisParameterType and OmitThisParameter in Typescript
type ThisParameterType<T> = T extends (this: infer U, ...args: any[]) => any ? U : unknown; type OmitThisParameter<T> = T extends (this: any, ...args: infer A) => infer R ? (...args: A) => R : T; // built-in utility type
ThisParameterType extracts the type of this from a function type.
OmitThisParameter creates a new function type without the this parameter.
this from the greet function.function greet(this: {name: string}, greeting: string) { return `${greeting}, ${this.name}`; } type ThisType = ThisParameterType<typeof greet>; // ThisType is { name: string }
this parameter.type NoThisGreet = OmitThisParameter<typeof greet>; const greetNoThis: NoThisGreet = (greeting) => `Hello, ${greeting}`; // greetNoThis does not expect a <code>this</code> parameter
this and binds a new context.const obj = { name: 'Alice', greet }; const greetFn = obj.greet; // Using OmitThisParameter to call greet without <code>this</code> const greetWithoutThis: OmitThisParameter<typeof greet> = greetFn.bind({ name: 'Bob' }); console.log(greetWithoutThis('Hi'));
This program shows how to get the this type and create a version of the function without this. It calls the original with this and the new one without.
function showAge(this: { age: number }, prefix: string) { return `${prefix} ${this.age}`; } // Extract the type of this type AgeThis = ThisParameterType<typeof showAge>; // Create a function type without this const showAgeNoThis: OmitThisParameter<typeof showAge> = (prefix) => { // We can't use this here, so just return a fixed string return `${prefix} unknown`; }; const person = { age: 30, showAge }; console.log(showAge.call(person, 'Age:')); console.log(showAgeNoThis('Age:'));
ThisParameterType only works on function types that explicitly declare this.
OmitThisParameter is useful to convert methods into normal functions.
These utilities help avoid errors when this is used incorrectly.
ThisParameterType extracts the this type from a function.
OmitThisParameter removes the this parameter from a function type.
They help manage this safely in TypeScript functions.
Practice
What does ThisParameterType do in TypeScript?
Solution
Step 1: Understand the purpose of
This utility type extracts the type of theThisParameterTypethisparameter from a function type if it exists.Step 2: Compare with other utilities
OmitThisParameterremoves thethisparameter, butThisParameterTypeextracts it instead.Final Answer:
Extracts the type ofthisfrom a function type -> Option BQuick Check:
ThisParameterTypeextractsthistype [OK]
- Confusing extraction with removal
- Thinking it changes return type
- Assuming it adds
thisparameter
Which of the following is the correct syntax to use OmitThisParameter on a function type fn?
type NewFn = ?
Solution
Step 1: Identify correct generic syntax
OmitThisParameteris a generic type, so it requires angle brackets with a type inside, like<typeof fn>.Step 2: Check the options
Only OmitThisParameter<typeof fn> uses the correct generic syntax withtypeof fnto get the function type.Final Answer:
OmitThisParameter<typeof fn> -> Option DQuick Check:
Generic types use <> with typeof for functions [OK]
- Using parentheses instead of angle brackets
- Passing function call instead of type
- Omitting typeof keyword
Consider this TypeScript code:
function greet(this: { name: string }, greeting: string) {
return `${greeting}, ${this.name}!`;
}
type GreetNoThis = OmitThisParameter<typeof greet>;
const greetNoThis: GreetNoThis = (greeting) => greet.call({ name: 'Alice' }, greeting);
console.log(greetNoThis('Hello'));What is the output?
Solution
Step 1: Understand
It removes theOmitThisParametereffectthisparameter fromgreet, sogreetNoThisonly takesgreeting.Step 2: Analyze the call
greetNoThiscallsgreet.callwith an explicitthisobject havingname: 'Alice', so the greeting uses 'Alice'.Final Answer:
"Hello, Alice!" -> Option AQuick Check:
OmitThisParameter removesthis, call sets it explicitly [OK]
this, call sets it manually [OK]- Assuming
thisis undefined inside greet - Expecting type error on assignment
- Thinking greetNoThis needs
thisparameter
Find the error in this TypeScript code using ThisParameterType:
function show(this: { id: number }) {
console.log(this.id);
}
type ShowThis = ThisParameterType<typeof show>;
const idValue: ShowThis = 5;Solution
Step 1: Understand
It extracts theThisParameterTypeoutputthistype, which is{ id: number }here, not a number.Step 2: Check assignment
Assigning5(a number) toShowThis(an object type) causes a type error.Final Answer:
Assigning a number toShowThisis incorrect -> Option AQuick Check:
Type mismatch: object expected, number given [OK]
this type matches assigned value [OK]- Thinking
ThisParameterTypereturns a primitive - Ignoring type mismatch errors
- Confusing
thisparameter presence
You have a method with a this parameter and want to create a version without this that can be called with just the normal arguments. Which TypeScript utility types would you combine to extract the this type and create a new function type without this?
Solution
Step 1: Extract the
thistypeThisParameterTypeextracts thethistype from the method.Step 2: Remove the
thisparameterOmitThisParametercreates a new function type without thethisparameter.Final Answer:
UseThisParameterTypeto getthistype andOmitThisParameterto removethis-> Option CQuick Check:
Extract then omitthisfor safe function types [OK]
this first, then omit it for new function type [OK]- Trying to omit
thisbefore extracting it - Using unrelated utility types
- Confusing order of operations
Ready to prove it?
Pass the test and this pattern moves unknown
