What if you could rename object keys everywhere in your code with just one simple change?
Why Key remapping with as clause in Typescript? - Purpose & Use Cases
Imagine you have an object with many keys, but you want to rename some keys to new names manually everywhere in your code.
For example, you have a user object with keys like firstName and lastName, but you want to use name instead of firstName.
Manually renaming keys everywhere is slow and error-prone.
You might forget to rename some keys or make typos, causing bugs.
Also, if the object has many keys, renaming each one by hand is tiring and messy.
Key remapping with the as clause lets you rename keys easily and safely in one place.
This way, you can create a new object type with keys renamed as you want, without changing the original object.
It saves time, reduces mistakes, and keeps your code clean.
type User = { firstName: string; lastName: string };
const user: User = { firstName: 'Alice', lastName: 'Smith' };
const newUser = { name: user.firstName, lastName: user.lastName };type User = { firstName: string; lastName: string };
type NewUser = { [K in keyof User as K extends 'firstName' ? 'name' : K]: User[K] };
const user: User = { firstName: 'Alice', lastName: 'Smith' };
const newUser: NewUser = { name: user.firstName, lastName: user.lastName };This lets you transform object keys flexibly and safely, enabling clearer and more maintainable code.
When working with data from an API that uses different key names than your app, you can remap keys to match your app's naming style easily.
Manual key renaming is slow and error-prone.
Key remapping with as clause automates renaming keys safely.
This keeps code clean and reduces bugs.
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
