0
0
Typescriptprogramming~20 mins

Higher-order function types in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Higher-order Function Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a higher-order function returning a function
What is the output of the following TypeScript code?
Typescript
function multiplier(factor: number): (input: number) => number {
  return (input: number) => input * factor;
}
const double = multiplier(2);
console.log(double(5));
Aundefined
B10
CTypeError
D25
Attempts:
2 left
💡 Hint
Think about what the returned function does with the input.
🧠 Conceptual
intermediate
2:00remaining
Type of a function that takes a function and returns a function
Which TypeScript type correctly describes a function that takes a function (number => string) and returns a function (string => number)?
A(fn: (x: number) => string) => (y: string) => number
B(fn: (x: string) => string) => (y: number) => number
C(fn: (x: number) => number) => (y: string) => string
D(fn: (x: string) => number) => (y: number) => string
Attempts:
2 left
💡 Hint
Look carefully at the input and output types of the inner functions.
🔧 Debug
advanced
2:00remaining
Identify the error in higher-order function type usage
What error does this TypeScript code produce?
Typescript
function compose(f: (a: number) => string, g: (b: string) => boolean): (x: number) => boolean {
  return (x: number) => g(f(x));
}
const result = compose((n) => n.toString(), (s) => s.length > 2)(10);
console.log(result);
ANo error, outputs true
BTypeError at runtime
CTypeScript compile error: Argument of type '(n: number) => string' is not assignable to parameter of type '(a: number) => string'
DTypeScript compile error: Type 'boolean' is not assignable to type 'string'
Attempts:
2 left
💡 Hint
Check if the function types match and the composition is valid.
📝 Syntax
advanced
2:00remaining
Which option causes a syntax error in higher-order function type declaration?
Which of the following TypeScript function type declarations is syntactically invalid?
Arebmun >= )gnirts :y( >= )gnirts >= )rebmun :x( :nf(
B(fn: (x: number) => string) => (y: string) => number
C(fn: (x: number) => string) => (y string) => number
Dfn: (x: number) => string) => (y: string) => number
Attempts:
2 left
💡 Hint
Look for missing punctuation or keywords in the function parameter list.
🚀 Application
expert
2:00remaining
Determine the output count of a higher-order function returning multiple functions
Consider this TypeScript code. How many functions are returned and stored in the array 'funcs'?
Typescript
function createFuncs(): Array<() => number> {
  const funcs = [];
  for (let i = 0; i < 3; i++) {
    funcs.push(() => i);
  }
  return funcs;
}
const funcs = createFuncs();
console.log(funcs.length);
ATypeError
B0
C1
D3
Attempts:
2 left
💡 Hint
Count how many times the loop runs and how many functions are pushed.