Bird
Raised Fist0
Typescriptprogramming~5 mins

Mapped type with conditional types in Typescript - Time & Space Complexity

Choose your learning style10 modes available
Time Complexity: Mapped type with conditional types
O(n)
Understanding Time Complexity

We want to understand how the time needed to create a new type changes as the input type grows.

How does the process of mapping and checking conditions for each property scale?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


type FilterStrings<T> = {
  [K in keyof T]: T[K] extends string ? K : never
}[keyof T];

// This creates a union of keys whose values are strings
    

This code creates a new type by checking each property of T and keeping only those keys whose values are strings.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Iterating over each key in the input type T.
  • How many times: Once for every property key in T.
How Execution Grows With Input

As the number of properties in T grows, the number of checks grows the same way.

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

Pattern observation: The work grows directly with the number of properties, one check per property.

Final Time Complexity

Time Complexity: O(n)

This means the time to create the mapped type grows linearly with the number of properties in the input type.

Common Mistake

[X] Wrong: "The conditional check inside the mapped type runs multiple times per property or grows faster than the number of properties."

[OK] Correct: Each property is checked exactly once, so the total work grows only with the number of properties, not more.

Interview Connect

Understanding how mapped types with conditions scale helps you reason about type transformations and their cost, a useful skill for writing efficient and maintainable TypeScript code.

Self-Check

"What if we nested another mapped type inside this one? How would the time complexity change?"

Practice

(1/5)
1. What does a mapped type with conditional types do in TypeScript?
type Example<T> = { [K in keyof T]: T[K] extends string ? number : boolean }
easy
A. It creates a new type where string properties become number and others become boolean.
B. It creates a new type with all properties removed.
C. It changes all properties to string type.
D. It only keeps properties that are strings.

Solution

  1. Step 1: Understand mapped types

    Mapped types loop over each key in the original type to create a new type with transformed properties.
  2. Step 2: Apply conditional types

    The conditional type checks if the property type extends string; if yes, it changes to number, else to boolean.
  3. Final Answer:

    It creates a new type where string properties become number and others become boolean. -> Option A
  4. Quick Check:

    Mapped + conditional changes types based on condition [OK]
Hint: Mapped types loop keys; conditional types pick types by condition [OK]
Common Mistakes:
  • Thinking it removes properties instead of transforming them
  • Assuming all types become string
  • Confusing conditional type logic
2. Which of the following is the correct syntax for a mapped type with a conditional type in TypeScript?
easy
A. type NewType<T> = { [K in T]: T[K] extends number ? string : boolean }
B. type NewType<T> = { [K in keyof T]: T[K] extends number ? string : boolean }
C. type NewType<T> = { [K of keyof T]: T[K] extends number ? string : boolean }
D. type NewType<T> = { K in keyof T: T[K] ? string : boolean }

Solution

  1. Step 1: Check mapped type syntax

    The correct syntax uses square brackets with 'in' keyword: [K in keyof T].
  2. Step 2: Verify conditional type usage

    The conditional type is written as T[K] extends number ? string : boolean, which is correct.
  3. Final Answer:

    type NewType<T> = { [K in keyof T]: T[K] extends number ? string : boolean } -> Option B
  4. Quick Check:

    Mapped type syntax = [K in keyof T] [OK]
Hint: Mapped types use [K in keyof T], not 'of' or missing brackets [OK]
Common Mistakes:
  • Using 'K in T' instead of 'K in keyof T'
  • Using 'K of keyof T' which is invalid
  • Missing square brackets around K in mapped type
3. Given the code:
type Flags = { a: string; b: number; c: boolean };
type Result = { [K in keyof Flags]: Flags[K] extends number ? 'num' : 'not-num' };
const test: Result = { a: 'not-num', b: 'num', c: 'not-num' };

What is the type of test.b?
medium
A. 'not-num'
B. number
C. 'num'
D. boolean

Solution

  1. Step 1: Analyze the mapped type Result

    For each key in Flags, if the property type is number, Result's property is 'num', else 'not-num'.
  2. Step 2: Check property b in Flags

    b is number, so Result['b'] is 'num'.
  3. Final Answer:

    'num' -> Option C
  4. Quick Check:

    Flags['b'] is number, so Result['b'] = 'num' [OK]
Hint: Check original type, then apply conditional mapping [OK]
Common Mistakes:
  • Confusing original property type with mapped type
  • Assuming test.b is number instead of 'num'
  • Ignoring conditional type logic
4. Identify the error in this mapped type:
type Fix<T> = { [P in keyof T]: T[P] extends string ? number : }
medium
A. Incorrect use of keyof operator
B. Cannot use conditional types inside mapped types
C. Square brackets missing around P
D. Missing type after colon in conditional type

Solution

  1. Step 1: Check conditional type syntax

    Conditional types require both true and false branches separated by colon.
  2. Step 2: Identify missing false branch

    The code ends with ':' but no type after it, causing syntax error.
  3. Final Answer:

    Missing type after colon in conditional type -> Option D
  4. Quick Check:

    Conditional types need both branches [OK]
Hint: Conditional types need both true and false parts [OK]
Common Mistakes:
  • Leaving out false branch in conditional type
  • Misplacing square brackets in mapped type
  • Thinking keyof is wrong here
5. How can you create a mapped type that keeps only the keys of an object whose values are assignable to a specific type, for example, only keys with string values?
Choose the correct mapped type:
hard
A. type FilterStrings<T> = { [K in keyof T as T[K] extends string ? K : never]: T[K] }
B. type FilterStrings<T> = { [K in keyof T]: T[K] extends string ? never : K }
C. type FilterStrings<T> = { [K in keyof T]: T[K] extends string ? string : never }
D. type FilterStrings<T> = { [K in keyof T]: K extends string ? T[K] : never }

Solution

  1. Step 1: Understand key remapping in mapped types

    Using 'as' allows filtering keys by condition, replacing keys with 'never' to omit them.
  2. Step 2: Check each option

    type FilterStrings<T> = { [K in keyof T as T[K] extends string ? K : never]: T[K] } correctly uses 'as' with conditional to keep keys where T[K] extends string; others become never (excluded).
  3. Final Answer:

    type FilterStrings<T> = { [K in keyof T as T[K] extends string ? K : never]: T[K] } -> Option A
  4. Quick Check:

    Key remapping with 'as' filters keys [OK]
Hint: Use 'as' with conditional to filter keys in mapped types [OK]
Common Mistakes:
  • Not using 'as' for key filtering
  • Returning never as value instead of key
  • Confusing key and value conditions
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