0
0
Typescriptprogramming~20 mins

Type alias for functions in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Function Type Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this TypeScript function using a type alias?

Consider the following TypeScript code that uses a type alias for a function. What will be printed when greet('Alice') is called?

Typescript
type Greet = (name: string) => string;

const greet: Greet = (name) => {
  return `Hello, ${name}!`;
};

console.log(greet('Alice'));
A"Hello, Alice!"
BError: greet is not a function
Cundefined
DHello, Alice!
Attempts:
2 left
💡 Hint

Remember that console.log prints the string without quotes.

🧠 Conceptual
intermediate
2:00remaining
Which type alias correctly describes a function that takes two numbers and returns a boolean?

Choose the correct TypeScript type alias for a function that takes two number arguments and returns a boolean.

Atype Compare = (a: string, b: string) => boolean;
Btype Compare = (a: number, b: number) => number;
Ctype Compare = (a: number, b: number) => boolean;
Dtype Compare = (a: number) => boolean;
Attempts:
2 left
💡 Hint

Check the parameter types and the return type carefully.

🔧 Debug
advanced
2:00remaining
What error does this TypeScript code produce?

Examine the following TypeScript code using a type alias for a function. What error will the TypeScript compiler report?

Typescript
type Operation = (x: number, y: number) => number;

const add: Operation = (x, y) => {
  return x + y;
};

const result: string = add(5, 3);
ACannot assign to 'result' because it is a constant.
BType 'number' is not assignable to type 'string'.
CParameter 'y' implicitly has an 'any' type.
DNo error, code compiles successfully.
Attempts:
2 left
💡 Hint

Look at the type of result and the return type of add.

📝 Syntax
advanced
2:00remaining
Which option correctly defines a type alias for a function with optional parameters?

Which of the following TypeScript type aliases correctly defines a function type that takes one required string parameter and one optional number parameter, returning void?

Atype Func = (name: string, age?: number) => void;
Btype Func = (name?: string, age: number) => void;
Ctype Func = (name: string, age: number?) => void;
Dtype Func = (name: string, age: number | undefined) => void;
Attempts:
2 left
💡 Hint

Optional parameters are marked with a question mark after the parameter name.

🚀 Application
expert
2:00remaining
How many items are in the resulting array after applying this function type alias?

Given the following TypeScript code, how many items will the results array contain after execution?

Typescript
type Transformer = (input: string) => string;

const transformers: Transformer[] = [
  (s) => s.toUpperCase(),
  (s) => s + "!",
  (s) => s.repeat(2)
];

const input = "hi";
const results = transformers.map(fn => fn(input));
A3
B1
C0
DError at runtime
Attempts:
2 left
💡 Hint

Count how many functions are in the transformers array.