What is the template literal type key remapping in TypeScript?
Why Interviewers Ask This
Advanced questions like this reveal whether a candidate has internalized TypeScript deeply enough to make architectural decisions. Strong answers demonstrate both breadth and depth of experience.
Answer
Key remapping in mapped types (TypeScript 4.1) uses the as clause to transform property names during mapping. Syntax: type Mapped = { [K in keyof T as NewKeyType]: T[K] }. Generate getter methods: type Getters<T> = { [K in keyof T as `get\${Capitalize<string & K>}`]: () => T[K] };. For interface User { name: string; age: number; }, Getters<User> produces { getName(): string; getAge(): number; }. Filter properties during mapping by using never: type NoFunctions<T> = { [K in keyof T as T[K] extends Function ? never : K]: T[K] }; — remapping to never removes that key from the result. Combine with template literals for prefix/suffix transformations: type Prefixed<T, P extends string> = { [K in keyof T as `\${P}\${string & K}`]: T[K] };. Key remapping enables advanced type transformations like generating event handler types, API endpoint types, and form field types from data model types.
Pro Tip
Back up your answer with a specific project or situation. Saying 'In my last TypeScript project, I used this when...' immediately makes your answer more credible and memorable.
Previous
What is excess property checking in TypeScript?
Next
What is the infer keyword with conditional types in depth?
More TypeScript Questions
View all →- Advanced What is structural typing in TypeScript?
- Advanced What are branded types (opaque types) in TypeScript?
- Advanced What is the Variance annotation in TypeScript 4.7?
- Advanced What is the satisfies operator used for in TypeScript?
- Advanced How do you implement a deep partial type in TypeScript?