How do you rename keys in a mapped type using the as clause?
Add as after the key in a mapped type, like { [K in keyof T as `get${Capitalize<K & string>}`]: () => T[K] }. The expression after as produces the new key name, and mapping a key to never removes it.
Explain it simply
A school register lists students by roll number, but a new report wants them by "Student-" plus the number. The as clause is that renaming step inside the mapping loop.
You can build a getter type from a plain data type: for name you get getName(). And if the key expression becomes never, that property is dropped, which gives you a way to filter keys.
Example
interface Person { name: string; age: number; secret: boolean }
type Getters<T> = {
[K in keyof T as `get${Capitalize<K & string>}`]: () => T[K];
};
type PersonGetters = Getters<Person>;
// { getName: () => string; getAge: () => number; getSecret: () => boolean }
// Filter: drop keys whose value is boolean
type WithoutBooleans<T> = {
[K in keyof T as T[K] extends boolean ? never : K]: T[K];
};
type Public = WithoutBooleans<Person>;
// { name: string; age: number }What to say in the interview
"The as clause in a mapped type remaps each key, usually with a template literal type, so I can generate getter or event-handler names from a data type. Remapping a key to never removes it, which gives me key filtering."