Challenge - 5 Problems
Parameter Type Annotation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a function with parameter type annotations
What is the output of this TypeScript code when calling
greet('Alice')?Typescript
function greet(name: string): string { return `Hello, ${name}!`; } console.log(greet('Alice'));
Attempts:
2 left
💡 Hint
Look at how the parameter
name is typed and how the function returns a string.✗ Incorrect
The function
greet takes a string parameter and returns a greeting string. Calling greet('Alice') returns 'Hello, Alice!'.❓ Predict Output
intermediate2:00remaining
Effect of wrong parameter type annotation
What happens when you call
square('5') with this function?Typescript
function square(num: number): number { return num * num; } console.log(square('5' as any));
Attempts:
2 left
💡 Hint
The argument is forced to
any type, so the function runs but with a string.✗ Incorrect
The string '5' is coerced to number 5 during multiplication, so the output is 25.
🔧 Debug
advanced2:00remaining
Identify the error in parameter type annotation
What error does this TypeScript code produce?
Typescript
function add(a: number, b: string): number { return a + b; }
Attempts:
2 left
💡 Hint
Check the return type and the expression inside the function.
✗ Incorrect
The function promises to return a number but tries to add a number and a string, resulting in a string. This conflicts with the return type.
📝 Syntax
advanced2:00remaining
Correct parameter type annotation syntax
Which option shows the correct way to annotate a function parameter as an optional string in TypeScript?
Attempts:
2 left
💡 Hint
Optional parameters use a question mark after the parameter name.
✗ Incorrect
In TypeScript, optional parameters are marked with a question mark after the parameter name, like
name?: string.🚀 Application
expert3:00remaining
Determine the type of parameter in a complex function
Given this function, what is the type of the parameter
callback?Typescript
function processData(callback: (data: string, count: number) => boolean): void { const result = callback('test', 3); console.log(result); }
Attempts:
2 left
💡 Hint
Look at the arrow function type annotation inside the parameter.
✗ Incorrect
The parameter
callback is a function that accepts a string and a number and returns a boolean.