Interview Q&A / TypeScript

AdvancedQuick answer1 min readUpdated 3 Oct 2026

How do you rename keys in a mapped type using the as clause?

SHORT ANSWER

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

ts
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."

#mapped-types#template-literal-types

Related questions

Practise more TypeScript questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open TypeScript quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.