📖 Scenario: You are creating a simple program to store and display information about a book in a library system.
🎯 Goal: Learn how to use explicit type annotations in TypeScript to define the types of variables and function parameters.
📋 What You'll Learn
Create variables with explicit type annotations
Define a function with typed parameters and return type
Use the variables and function to display information
💡 Why This Matters
🌍 Real World
Explicit type annotations help developers understand what kind of data variables and functions expect, reducing bugs in real-world applications like library systems.
💼 Career
Many programming jobs require writing clear, typed code to maintain large codebases and collaborate with teams effectively.
Progress0 / 4 steps
1
Create variables with explicit type annotations
Create a variable called title with type string and value "The Great Gatsby". Create a variable called year with type number and value 1925.
Typescript
Hint
Use let variableName: type = value; to declare variables with explicit types.
2
Add a variable with explicit boolean type
Create a variable called available with type boolean and value true.
Typescript
Hint
Boolean variables can be declared with type boolean and values true or false.
3
Create a function with typed parameters and return type
Create a function called getBookInfo that takes parameters title (type string), year (type number), and available (type boolean). The function should return a string combining these values in the format: "Title (Year) - Available" or "Title (Year) - Not Available" depending on the available value.
Typescript
Hint
Use function functionName(param: type, ...): returnType { ... } to define typed functions.
4
Display the book information using the function
Use console.log to print the result of calling getBookInfo with the variables title, year, and available.
Typescript
Hint
Call the function with the variables and print the result using console.log.
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.