TypeScript · Middle · Question 28 of 75
How do you rename keys in a mapped type?
type Getters<T> = {
[K in keyof T as `get${Capitalize<string & K>}`]: () => T[K];
};- ✓Correct answer: With
asafterin, using a template literal - 2It's impossible - keys in a mapped type always stay the same
- 3With a
rename()function from the standard type library
Explanation
Key remapping (TS 4.1+): Getters<{ name: string }> → { getName: () => string }. If the expression after as produces never, the key is dropped - that's how properties are filtered by value type. string & K is needed because keys can be symbols or numbers.