Bird
Raised Fist0
Typescriptprogramming~10 mins

Explicit type annotations in Typescript - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Explicit type annotations
Declare variable
↓
Add explicit type annotation
↓
Assign value matching type
↓
Use variable safely
↓
Compile-time check for type errors
↓
Run program if no errors
This flow shows how declaring a variable with an explicit type helps catch errors before running the program.
Execution Sample
Typescript
let age: number;
age = 25;
console.log(age);
Declare a variable with type number, assign a number, then print it.
Execution Table
StepActionVariableType AnnotationValueResult
1Declare variableagenumberundefinedVariable 'age' created with type number, no value yet
2Assign valueagenumber25Value 25 assigned to 'age', matches type number
3Print valueagenumber25Output: 25
4End---Program ends successfully
💡 No type errors, program runs and prints 25
Variable Tracker
VariableStartAfter Step 2Final
ageundefined2525
Key Moments - 2 Insights
Why do we write ': number' after 'age'?
The ': number' tells TypeScript that 'age' must always hold a number. This helps catch mistakes early, as shown in step 2 where the value assigned must match this type.
What happens if we assign a string to 'age'?
TypeScript will show an error before running the program because the assigned value does not match the declared type 'number'. This prevents bugs.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'age' after step 1?
Anull
B25
Cundefined
Dnumber
💡 Hint
Check the 'Value' column in row for step 1 in the execution table.
At which step does 'age' get assigned the value 25?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the 'Action' and 'Value' columns in the execution table.
If we change the type annotation to ': string' but assign 25, what happens?
AProgram runs and prints 25
BTypeScript shows a type error before running
CValue changes to '25' string automatically
DNo effect, code runs normally
💡 Hint
Refer to the key moment about assigning wrong types and the compile-time check step.
Concept Snapshot
Explicit type annotations in TypeScript:
- Syntax: let variableName: type;
- Forces variable to hold only that type
- Helps catch errors before running
- Example: let age: number;
- Assigning wrong type causes compile error
Full Transcript
This example shows how to declare a variable with an explicit type annotation in TypeScript. First, we declare 'age' with type 'number'. Then we assign the value 25 to it. The program prints 25. The explicit type helps TypeScript check that only numbers are assigned to 'age'. If we try to assign a different type, TypeScript will show an error before running the program. This prevents bugs and makes code safer.

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

  1. Step 1: Understand what explicit type annotations do

    Explicit type annotations specify the exact type for variables or functions in TypeScript.
  2. Step 2: Identify the purpose of explicit typing

    This helps TypeScript catch mistakes and makes the code easier to understand.
  3. Final Answer:

    To tell TypeScript exactly what type a variable or function uses -> Option A
  4. Quick Check:

    Explicit type annotations = specify exact types [OK]
Hint: Explicit types tell TS variable types clearly [OK]
Common Mistakes:
  • Thinking explicit types speed up runtime
  • Confusing type annotations with type conversion
  • Believing explicit types disable type checking
2. Which of the following is the correct syntax for explicitly typing a variable as a string in TypeScript?
easy
A. let name = string;
B. let name: string;
C. let name = "string";
D. let name: "string";

Solution

  1. Step 1: Recall TypeScript variable type annotation syntax

    Type annotations use a colon after the variable name followed by the type.
  2. Step 2: Match the correct syntax

    let name: string; uses let name: string; which is the correct way to declare a string type.
  3. Final Answer:

    let name: string; -> Option B
  4. Quick Check:

    Variable: type syntax = correct [OK]
Hint: Use colon after variable name for type [OK]
Common Mistakes:
  • Using equals sign instead of colon for type
  • Putting type inside quotes incorrectly
  • Assigning type as a value instead of annotation
3. What will be the output of this TypeScript code?
let age: number = 30;
age = "thirty";
console.log(age);
medium
A. Type error at compile time
B. thirty
C. 30
D. undefined

Solution

  1. Step 1: Check the variable type and assignment

    The variable age is explicitly typed as number, but assigned a string "thirty" later.
  2. Step 2: Understand TypeScript's type checking

    TypeScript will raise a compile-time error because the string assignment violates the number type annotation.
  3. Final Answer:

    Type error at compile time -> Option A
  4. Quick Check:

    Assigning wrong type = compile error [OK]
Hint: Assign only matching types to annotated variables [OK]
Common Mistakes:
  • Assuming code runs ignoring type mismatch
  • Thinking TypeScript converts types automatically
  • Expecting runtime error instead of compile error
4. Identify the error in this TypeScript code snippet:
function greet(name: string): void {
  console.log("Hello, " + name);
}
greet(42);
medium
A. Variable name is not declared
B. Missing return type annotation
C. Function body syntax error
D. Calling function with wrong argument type

Solution

  1. Step 1: Check function parameter type

    The function greet expects a string parameter named name.
  2. Step 2: Check the function call argument

    The call greet(42) passes a number instead of a string, causing a type mismatch error.
  3. Final Answer:

    Calling function with wrong argument type -> Option D
  4. 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

  1. Step 1: Understand the function's return structure

    The function returns an object mapping numbers to their squares, so keys and values are numbers.
  2. 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.
  3. Final Answer:

    Record<number, number> -> Option C
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard