Explicit type annotations help you tell the computer exactly what kind of data you expect. This makes your code clearer and helps catch mistakes early.
Explicit type annotations in Typescript
let variableName: type = value;The colon : is used to add the type after the variable name.
You can use explicit types for variables, function parameters, and return values.
age can only hold numbers.let age: number = 25;
name can only hold text (strings).let name: string = "Alice";
name and returns nothing (void).function greet(name: string): void { console.log(`Hello, ${name}!`); }
isActive can only be true or false.let isActive: boolean = true;
This program defines a function multiply that takes two numbers and returns their product. We use explicit type annotations for the parameters and the return type. Then we call the function and print the result.
function multiply(x: number, y: number): number { return x * y; } let result: number = multiply(4, 5); console.log(`The result is ${result}`);
Explicit types make your code safer and easier to read.
TypeScript can often guess types, but adding explicit types helps prevent errors.
You can use explicit types for arrays, objects, and more complex data too.
Explicit type annotations tell TypeScript exactly what type a variable or function uses.
They help catch mistakes and make code easier to understand.
Use them when you want clear, safe, and readable code.
Practice
explicit type annotations in TypeScript?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 AQuick Check:
Explicit type annotations = specify exact types [OK]
- Thinking explicit types speed up runtime
- Confusing type annotations with type conversion
- Believing explicit types disable type checking
Solution
Step 1: Recall TypeScript variable type annotation syntax
Type annotations use a colon after the variable name followed by the type.Step 2: Match the correct syntax
let name: string; useslet name: string;which is the correct way to declare a string type.Final Answer:
let name: string; -> Option BQuick Check:
Variable: type syntax = correct [OK]
- Using equals sign instead of colon for type
- Putting type inside quotes incorrectly
- Assigning type as a value instead of annotation
let age: number = 30; age = "thirty"; console.log(age);
Solution
Step 1: Check the variable type and assignment
The variableageis explicitly typed asnumber, but assigned a string "thirty" later.Step 2: Understand TypeScript's type checking
TypeScript will raise a compile-time error because the string assignment violates the number type annotation.Final Answer:
Type error at compile time -> Option AQuick Check:
Assigning wrong type = compile error [OK]
- Assuming code runs ignoring type mismatch
- Thinking TypeScript converts types automatically
- Expecting runtime error instead of compile error
function greet(name: string): void {
console.log("Hello, " + name);
}
greet(42);Solution
Step 1: Check function parameter type
The functiongreetexpects astringparameter namedname.Step 2: Check the function call argument
The callgreet(42)passes a number instead of a string, causing a type mismatch error.Final Answer:
Calling function with wrong argument type -> Option DQuick Check:
Argument type must match parameter type [OK]
- Ignoring parameter type annotations
- Assuming implicit type conversion
- Missing function return type is not an error here
function squareMap(nums: number[]): ??? {
const result = {};
for (const num of nums) {
result[num] = num * num;
}
return result;
}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'sRecord<number, number>type explicitly defines an object with number keys and number values, matching the function's output.Final Answer:
Record<number, number> -> Option CQuick Check:
Use Record for typed object maps [OK]
- Using string keys when keys are numbers
- Returning array instead of object
- Using index signature with wrong key type
Ready to prove it?
Pass the test and this pattern moves unknown
