Key remapping with as clause in Typescript - Time & Space Complexity
We want to understand how the time needed changes when we remap keys in an object using the as clause in TypeScript.
How does the work grow when the object has more keys?
Analyze the time complexity of the following code snippet.
type Original = {
a: number;
b: string;
c: boolean;
};
// Remap keys with 'as' clause
type Remapped = {
[K in keyof Original as `new_${string & K}`]: Original[K]
};
This code remaps each key of Original by adding a prefix new_ using the as clause.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Iterating over each key in the original object type.
- How many times: Once for each key in the object.
As the number of keys grows, the remapping work grows proportionally.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 key remaps |
| 100 | 100 key remaps |
| 1000 | 1000 key remaps |
Pattern observation: The work grows directly with the number of keys, doubling keys doubles work.
Time Complexity: O(n)
This means the time to remap keys grows linearly with the number of keys in the object.
[X] Wrong: "Remapping keys with as clause happens instantly regardless of object size."
[OK] Correct: Each key must be processed once, so more keys mean more work, not zero time.
Understanding how key remapping scales helps you reason about type transformations and their cost in larger codebases.
"What if we nested the remapping inside another mapped type? How would the time complexity change?"