Conditional Key Generation
What you'll learn: How to use conditional types inside mapped types to decide which keys get transformed—or included at all.
The Core Idea
You've learned to remap keys with as and template literals. Now we add conditional logic to those mappings. This lets you say: "Only transform this key if it meets a certain condition, otherwise exclude it (using never)."
Think of it like filtering a guest list while also customizing name tags. Not everyone gets in, and those who do might get different label styles based on their VIP status.
How It Works
Inside a mapped type's as clause, you can use a conditional type to decide the new key name—or return never to omit the property entirely.
Pattern:
type Conditional<T> = {
[K in keyof T as SomeCondition ? TransformedKey : never]: T[K]
}
When the condition is false, never removes that key from the resulting type.
Selective Template Literal Transformations
Combine this with template literals to transform only certain keys:
type User = {
name: string;
age: number;
isActive: boolean;
};
// Only prefix boolean properties with "check"
type PrefixBooleans<T> = {
[K in keyof T as T[K] extends boolean ? `check${Capitalize<string & K>}` : K]: T[K]
};
type Result = PrefixBooleans<User>;
// { name: string; age: number; checkIsActive: boolean }
Here, T[K] extends boolean checks each property's type. Only isActive gets the template literal transformation; others pass through unchanged.
Key Takeaway: Conditional types inside mapped type as clauses let you selectively apply template literal transformations to keys based on their value types, creating highly targeted type transformations.