Recall & Review
beginner
What is the purpose of type annotation on function parameters in TypeScript?
Type annotation on function parameters tells TypeScript what type of value the function expects for each parameter. This helps catch errors early and improves code clarity.
Click to reveal answer
beginner
How do you add a type annotation to a function parameter in TypeScript?
You add a colon (:) after the parameter name, followed by the type. For example: <code>function greet(name: string) { ... }</code> means the parameter <code>name</code> must be a string.Click to reveal answer
beginner
What happens if you pass a value of the wrong type to a function with typed parameters?
TypeScript will show an error during development, helping you fix the mistake before running the code. This prevents bugs caused by unexpected types.
Click to reveal answer
intermediate
Can function parameters have multiple types in TypeScript? How?
Yes, by using union types. For example: <code>function printId(id: number | string) { ... }</code> means <code>id</code> can be a number or a string.Click to reveal answer
intermediate
What is the difference between optional and required parameters in TypeScript functions?
Required parameters must be provided when calling the function. Optional parameters are marked with a question mark (?), like <code>function greet(name?: string) { ... }</code>, and can be omitted.Click to reveal answer
How do you specify that a function parameter must be a string in TypeScript?
✗ Incorrect
Option B correctly uses a colon after the parameter name to specify its type.
What will TypeScript do if you pass a number to a function expecting a string parameter?
✗ Incorrect
TypeScript shows an error during development (compile-time), preventing type mistakes.
How do you declare a function parameter that can accept either a number or a string?
✗ Incorrect
The pipe symbol (|) creates a union type allowing multiple types.
How do you mark a function parameter as optional in TypeScript?
✗ Incorrect
Adding a question mark (?) after the parameter name makes it optional.
Which of these is a correct function parameter type annotation?
✗ Incorrect
Option C correctly annotates both parameters with types using colons.
Explain how to add type annotations to function parameters in TypeScript and why it is useful.
Think about how you tell TypeScript what kind of data a function expects.
You got /4 concepts.
Describe how to make a function parameter optional and how to allow multiple types for a parameter.
Consider how to say a parameter can be missing or can be one of several types.
You got /3 concepts.