Recall & Review
beginner
What is a parameter type annotation in TypeScript?
A parameter type annotation specifies the expected type of a function's parameter, helping catch errors and improve 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. Example: <code>function greet(name: string) {}</code>Click to reveal answer
beginner
What happens if you pass a wrong type to a function parameter with a type annotation?
TypeScript will show a compile-time error, preventing the code from running until the type mismatch is fixed.
Click to reveal answer
beginner
Can you annotate multiple parameters with different types in a function?
Yes, each parameter can have its own type annotation. Example: <code>function add(a: number, b: number): number { return a + b; }</code>Click to reveal answer
beginner
Why are parameter type annotations helpful for beginners?
They guide you to use the right types, catch mistakes early, and make your code easier to understand and maintain.
Click to reveal answer
How do you write a parameter type annotation for a string parameter named 'text'?
✗ Incorrect
In TypeScript, the correct syntax is
parameterName: type, so text: string is correct.What error will TypeScript show if you call
greet(123) when function greet(name: string) is defined?✗ Incorrect
TypeScript checks types at compile time and will report that a number cannot be used where a string is expected.
Can you omit parameter type annotations in TypeScript?
✗ Incorrect
If you omit the type, TypeScript treats the parameter as 'any', which disables type checking for that parameter.
Which of these is a valid function with two parameters annotated with types?
✗ Incorrect
Option C uses correct TypeScript syntax for parameter type annotations.
What is the main benefit of parameter type annotations?
✗ Incorrect
Parameter type annotations help catch mistakes early by checking types during development.
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 /3 concepts.
Describe what happens if you pass a wrong type to a function parameter that has a type annotation.
Consider how TypeScript helps avoid bugs before running the program.
You got /3 concepts.