Consider the following TypeScript code that uses a type alias for a function. What will be printed when greet('Alice') is called?
type Greet = (name: string) => string; const greet: Greet = (name) => { return `Hello, ${name}!`; }; console.log(greet('Alice'));
Remember that console.log prints the string without quotes.
The function greet returns the string Hello, Alice!. When printed with console.log, the output is Hello, Alice! without quotes.
Choose the correct TypeScript type alias for a function that takes two number arguments and returns a boolean.
Check the parameter types and the return type carefully.
The function must take two numbers and return a boolean. Option C matches this exactly.
Examine the following TypeScript code using a type alias for a function. What error will the TypeScript compiler report?
type Operation = (x: number, y: number) => number; const add: Operation = (x, y) => { return x + y; }; const result: string = add(5, 3);
Look at the type of result and the return type of add.
The function add returns a number, but result is declared as a string. This causes a type mismatch error.
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?
Optional parameters are marked with a question mark after the parameter name.
Option A correctly marks age as optional. Option A makes name optional instead. Option A has invalid syntax. Option A uses a union type but does not make the parameter optional.
Given the following TypeScript code, how many items will the results array contain after execution?
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));
Count how many functions are in the transformers array.
There are 3 functions in the transformers array. The map method applies each function to the input string, so results will have 3 items.