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? let price: number = 10;
AThe variable price can only hold numbers.
BThe variable price can hold any type.
CThe variable price is a string.
DThe variable price is undefined.
✗ Incorrect
The explicit type annotation : number means price can only hold numbers.
How do you specify a function parameter must be a string?
Afunction greet(name string) {}
Bfunction greet(name) {}
Cfunction greet(string name) {}
Dfunction greet(name: string) {}
✗ Incorrect
You add a colon and the type after the parameter name: name: string.
What error will TypeScript show for this code? let age: number = 'twenty';
AError: Type 'string' is not assignable to type 'number'.
BNo error, it's valid.
CError: Missing type annotation.
DError: Variable age is undefined.
✗ Incorrect
Assigning a string to a variable declared as number causes a type error.
Why might you add explicit return type annotations to functions?
ATo make the function run faster.
BTo avoid writing function code.
CTo help TypeScript check the function returns the correct type.
DTo allow any return type.
✗ Incorrect
Explicit return types help TypeScript verify the function returns what you expect.
Which is a correct explicit type annotation for a boolean variable?
Alet isOpen = true: boolean;
Blet isOpen: boolean = true;
Clet isOpen: bool = true;
Dlet isOpen: Boolean = 'true';
✗ 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.
Practice
(1/5)
1. What is the main purpose of using explicit type annotations in TypeScript?
easy
A. To tell TypeScript exactly what type a variable or function uses
B. To make the code run faster
C. To automatically convert types at runtime
D. To remove all type checking
Solution
Step 1: Understand what explicit type annotations do
Explicit type annotations specify the exact type for variables or functions in TypeScript.
Step 2: Identify the purpose of explicit typing
This helps TypeScript catch mistakes and makes the code easier to understand.
Final Answer:
To tell TypeScript exactly what type a variable or function uses -> Option A
Quick Check:
Explicit type annotations = specify exact types [OK]
The function greet expects a string parameter named name.
Step 2: Check the function call argument
The call greet(42) passes a number instead of a string, causing a type mismatch error.
Final Answer:
Calling function with wrong argument type -> Option D
Quick Check:
Argument type must match parameter type [OK]
Hint: Match argument types to parameter annotations [OK]
Common Mistakes:
Ignoring parameter type annotations
Assuming implicit type conversion
Missing function return type is not an error here
5. You want to create a function that takes an array of numbers and returns an object mapping each number to its square. Which explicit type annotation correctly describes the return type?
function squareMap(nums: number[]): ??? {
const result = {};
for (const num of nums) {
result[num] = num * num;
}
return result;
}
hard
A. Map
B. { [key: string]: number }
C. Record
D. Array<number>
Solution
Step 1: Understand the function's return structure
The function returns an object mapping numbers to their squares, so keys and values are numbers.
Step 2: Choose the best explicit return type
TypeScript's Record<number, number> type explicitly defines an object with number keys and number values, matching the function's output.
Final Answer:
Record<number, number> -> Option C
Quick Check:
Use Record for typed object maps [OK]
Hint: Use Record for typed objects [OK]
Common Mistakes:
Using string keys when keys are numbers
Returning array instead of object
Using index signature with wrong key type
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.