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?"
Practice
What does the as clause do in a TypeScript mapped type?
Solution
Step 1: Understand mapped types
Mapped types create new types by transforming keys or values of an existing type.Step 2: Role of
Theasclauseasclause specifically allows renaming keys during this transformation.Final Answer:
It renames the keys of the original type to new keys. -> Option DQuick Check:
asclause = rename keys [OK]
as changes keys, not values [OK]- Thinking
aschanges values instead of keys - Confusing
aswith adding new keys - Assuming
asremoves keys
Which of the following is the correct syntax to rename keys in a mapped type using as?
type Original = { a: number; b: string };
type Renamed = { [K in keyof Original as ???]: Original[K] };Solution
Step 1: Understand template literal types in key remapping
To rename keys dynamically, template literals like`new_${K}`are used inside theasclause.Step 2: Check syntax correctness
Only`new_${K}`is valid syntax for renaming keys using template literals in TypeScript.Final Answer:
`new_${K}` -> Option BQuick Check:
Use template literals insideasfor renaming [OK]
- Using plus (+) operator instead of template literals
- Writing
as Kinside brackets incorrectly - Trying to cast keys with
as string
What is the resulting type of this code?
type User = { id: number; name: string };
type PrefixedUser = { [K in keyof User as `user_${K}`]: User[K] };Solution
Step 1: Analyze key remapping with template literals
The keysidandnameare renamed touserIdanduserNamerespectively using`user_${K}`.Step 2: Confirm values remain unchanged
The values for each key remain the same as inUser.Final Answer:
{ userId: number; userName: string } -> Option AQuick Check:
Keys renamed with prefix 'user_' [OK]
- Assuming keys stay the same
- Confusing camelCase with snake_case
- Expecting a syntax error
Identify the error in this mapped type:
type Data = { x: number; y: string };
type NewData = { [K in keyof Data as K.toUpperCase()]: Data[K] };Solution
Step 1: Check key remapping expression
The expressionK.toUpperCase()is a runtime string method and cannot be used in type-level key remapping.Step 2: Understand type-level string manipulation
TypeScript requires template literal types or conditional types for key transformations, not runtime methods.Final Answer:
Cannot usetoUpperCase()on type keys directly. -> Option AQuick Check:
Runtime methods disallowed in type keys [OK]
- Trying to call string methods on keys in types
- Confusing runtime and compile-time code
- Ignoring TypeScript's type system limits
Given this type, how would you create a new type that renames only keys starting with is by prefixing them with flag_, keeping others unchanged?
type Flags = { isActive: boolean; isAdmin: boolean; count: number };Solution
Step 1: Use conditional type in
The conditionasclauseK extends `is${string}`checks if the key starts with 'is'.Step 2: Rename keys conditionally
If true, rename key to`flag_${K}`, else keep original keyK.Step 3: Keep values unchanged
Values remain the same as inFlags.Final Answer:
type NewFlags = { [K in keyof Flags as K extends `is${string}` ? `flag_${K}` : K]: Flags[K] }; -> Option CQuick Check:
Conditional key renaming with template literals [OK]
as with template literals [OK]- Renaming all keys without condition
- Using runtime string methods in types
- Not preserving keys that don't match condition
Ready to prove it?
Pass the test and this pattern moves unknown
