Bird
Raised Fist0
Typescriptprogramming~5 mins

Explicit type annotations in Typescript - Time & Space Complexity

Choose your learning style10 modes available
Time Complexity: Explicit type annotations
O(n)
Understanding Time Complexity

We want to see how adding explicit type annotations affects the speed of running TypeScript code.

Does writing types change how fast the program runs?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


function sumArray(numbers: number[]): number {
  let total = 0;
  for (const num of numbers) {
    total += num;
  }
  return total;
}

const data = [1, 2, 3, 4, 5];
console.log(sumArray(data));
    

This code adds up all numbers in an array using explicit type annotations for the function and its parameter.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Looping through each number in the array.
  • How many times: Once for every item in the array.
How Execution Grows With Input

As the array gets bigger, the program does more additions, one for each number.

Input Size (n)Approx. Operations
1010 additions
100100 additions
10001000 additions

Pattern observation: The work grows directly with the number of items.

Final Time Complexity

Time Complexity: O(n)

This means the time to finish grows in a straight line as the input size grows.

Common Mistake

[X] Wrong: "Adding explicit type annotations makes the program slower at running."

[OK] Correct: Type annotations help during coding but are removed when running, so they do not affect speed.

Interview Connect

Understanding that type annotations help with code clarity but do not slow down the program shows you know how TypeScript works behind the scenes.

Self-Check

"What if we removed the explicit return type from the function? How would the time complexity change?"

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