0
0
Typescriptprogramming~10 mins

Higher-order function types in Typescript - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a function type that takes a number and returns a number.

Typescript
type NumberFunc = (x: number) => [1];
Drag options to blanks, or click blank then click option'
Aboolean
Bnumber
Cvoid
Dstring
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing a return type different from the input type.
Confusing parameter type with return type.
2fill in blank
medium

Complete the code to declare a higher-order function type that takes a function (number to number) and returns a function (number to number).

Typescript
type HigherOrder = (fn: (x: number) => number) => [1];
Drag options to blanks, or click blank then click option'
Avoid
Bstring
Cnumber
D(x: number) => number
Attempts:
3 left
💡 Hint
Common Mistakes
Using void as return type instead of a function type.
Confusing return type with parameter type.
3fill in blank
hard

Fix the error in the function type declaration to correctly type a function that takes a function and returns void.

Typescript
type Func = (callback: (msg: string) => void) => [1];
Drag options to blanks, or click blank then click option'
Avoid
Bstring
Cnumber
D(msg: string) => void
Attempts:
3 left
💡 Hint
Common Mistakes
Using a function type as return type instead of void.
Confusing parameter and return types.
4fill in blank
hard

Fill both blanks to declare a function type that takes a function returning a boolean and returns a function returning a string.

Typescript
type ComplexFunc = (check: () => [1]) => () => [2];
Drag options to blanks, or click blank then click option'
Aboolean
Bstring
Cnumber
Dvoid
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up the return types of the parameter and returned functions.
Using void instead of the correct types.
5fill in blank
hard

Fill all three blanks to declare a higher-order function type that takes a function (string to number), returns a function taking a number and returning a boolean.

Typescript
type HOF = (fn: (input: [1]) => [2]) => (arg: [3]) => boolean;
Drag options to blanks, or click blank then click option'
Astring
Bnumber
Dboolean
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping input and output types.
Using wrong types for the returned function's argument.