Recall & Review
beginner
What is an explicit type annotation in TypeScript?
An explicit type annotation is when you directly specify the type of a variable, function parameter, or return value using a colon and the type name, like <code>let age: number;</code>. It helps the compiler know exactly what type to expect.Click to reveal answer
beginner
Why use explicit type annotations instead of relying on TypeScript's type inference?
Explicit type annotations make your code clearer and easier to understand. They also help catch mistakes early by ensuring variables have the exact type you want, especially when the inferred type might be too broad or unclear.
Click to reveal answer
beginner
How do you add an explicit type annotation to a function parameter in TypeScript?
You add a colon and the type after the parameter name. 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 assign a value of the wrong type to a variable with an explicit type annotation?
TypeScript will show an error during compilation because the value does not match the declared type. This helps prevent bugs by catching type mistakes early.
Click to reveal answer
beginner
Show an example of explicit type annotation for a variable and a function return type.
Example:<br><code>let count: number = 5;</code><br>Here, <code>count</code> is explicitly a number.<br><br>Function with return type:<br><code>function getName(): string { return 'Alice'; }</code><br>This means the function must return a string.Click to reveal answer
What does this TypeScript code mean?<br>
let price: number = 10;✗ Incorrect
The explicit type annotation
: number means price can only hold numbers.How do you specify a function parameter must be a string?
✗ Incorrect
You add a colon and the type after the parameter name:
name: string.What error will TypeScript show for this code?<br>
let age: number = 'twenty';✗ Incorrect
Assigning a string to a variable declared as number causes a type error.
Why might you add explicit return type annotations to functions?
✗ Incorrect
Explicit return types help TypeScript verify the function returns what you expect.
Which is a correct explicit type annotation for a boolean variable?
✗ Incorrect
The correct type is
boolean (all lowercase), and the value must be true or false.Explain what explicit type annotations are and why they are useful in TypeScript.
Think about how telling the computer exactly what type a variable should be helps.
You got /4 concepts.
Write a simple TypeScript function with explicit type annotations for parameters and return type.
Use a function that takes a string and returns a string.
You got /4 concepts.