0
0
Typescriptprogramming~5 mins

Pick type in Typescript - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Pick type
O(n)
Understanding Time Complexity

Let's see how the time it takes to create a new type using Pick changes as the number of keys grows.

We want to know how the work grows when we pick more properties from a type.

Scenario Under Consideration

Analyze the time complexity of this TypeScript type operation.


type Original = {
  a: string;
  b: number;
  c: boolean;
  d: Date;
  e: string[];
};

type Picked = Pick;
    

This code creates a new type by selecting some keys from an existing type.

Identify Repeating Operations

What happens behind the scenes when Pick runs?

  • Primary operation: Iterating over the keys to select the chosen ones.
  • How many times: Once for each key you pick.
How Execution Grows With Input

As the number of keys to pick grows, the work grows too.

Input Size (number of keys)Approx. Operations
33 checks
1010 checks
100100 checks

Pattern observation: The work grows directly with the number of keys you pick.

Final Time Complexity

Time Complexity: O(n)

This means the time to create the picked type grows linearly with the number of keys selected.

Common Mistake

[X] Wrong: "Pick runs instantly no matter how many keys are selected."

[OK] Correct: Even though it's fast, Pick must check each key to include it, so more keys mean more work.

Interview Connect

Understanding how type operations scale helps you reason about code maintainability and performance in larger projects.

Self-Check

What if we used a type that picks keys conditionally instead of explicitly? How would that affect time complexity?