0
0
Typescriptprogramming~20 mins

ThisParameterType and OmitThisParameter in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
ThisParameterType and OmitThisParameter Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of function using ThisParameterType
What is the output of this TypeScript code using ThisParameterType?
Typescript
function greet(this: {name: string}, greeting: string) {
  return `${greeting}, ${this.name}!`;
}

const greetFn: (greeting: string) => string = greet;

const obj = { name: "Alice", greet: greetFn };

console.log(greetFn("Hello"));
ATypeError: Cannot read property 'name' of undefined
B"Hello, Alice!"
C"Hello, undefined!"
DSyntaxError: Unexpected token
Attempts:
2 left
💡 Hint
Remember that ThisParameterType extracts the type of this parameter, but the function is assigned without binding this.
Predict Output
intermediate
2:00remaining
Effect of OmitThisParameter on function type
What is the type of newFn after applying OmitThisParameter to fn in this code?
Typescript
function fn(this: {x: number}, y: number): number {
  return this.x + y;
}

type FnNoThis = OmitThisParameter<typeof fn>;

const newFn: FnNoThis = (y) => fn.call({x: 10}, y);

console.log(newFn(5));
ANaN
BTypeError: Cannot read property 'x' of undefined
C15
DSyntaxError
Attempts:
2 left
💡 Hint
OmitThisParameter removes the this parameter from the function type, so newFn only takes y.
🔧 Debug
advanced
2:00remaining
Why does this code cause a TypeScript error?
This code tries to assign a function with a this parameter to a variable without it. Why does TypeScript show an error here?
Typescript
function show(this: {name: string}) {
  console.log(this.name);
}

const fn: () => void = show;
ABecause the function <code>show</code> requires a <code>this</code> parameter but <code>fn</code> type does not include it
BBecause <code>show</code> has a syntax error in the <code>this</code> parameter
CBecause <code>fn</code> is missing a return type
DBecause <code>show</code> is not declared with an arrow function
Attempts:
2 left
💡 Hint
Check the function types and how this parameters affect assignability.
🧠 Conceptual
advanced
1:30remaining
Purpose of ThisParameterType utility type
What does the TypeScript utility type ThisParameterType do?
ABinds the <code>this</code> parameter to a specific object
BExtracts the type of the <code>this</code> parameter from a function type
CRemoves the <code>this</code> parameter from a function type
DConverts a function to an arrow function
Attempts:
2 left
💡 Hint
Think about what part of the function type ThisParameterType extracts.
Predict Output
expert
2:30remaining
Output of combined ThisParameterType and OmitThisParameter usage
What is the output of this TypeScript code that uses both ThisParameterType and OmitThisParameter?
Typescript
function multiply(this: {factor: number}, value: number): number {
  return this.factor * value;
}

type FactorType = ThisParameterType<typeof multiply>;

const obj: FactorType = { factor: 3 };

type MultiplyNoThis = OmitThisParameter<typeof multiply>;

const multiplyNoThis: MultiplyNoThis = (value) => multiply.call(obj, value);

console.log(multiplyNoThis(5));
ATypeError: Cannot read property 'factor' of undefined
BNaN
C5
D15
Attempts:
2 left
💡 Hint
Look at how obj is typed using ThisParameterType and how multiplyNoThis calls multiply with obj as this.