Question presented to candidate:
"In a mapped type you can write as after the key. What does that do? Show me how you would generate getter names like getName from a Person type, and how you would drop some keys."
What a strong answer should cover:
- The
asclause,{ [K in keyof T as NewKey]: ... }, computes the output key from each input key; added in TypeScript 4.1 together with template literal types - Renaming with template literal types and the intrinsic string helpers:
`get${Capitalize<string & K>}`, and whystring & Kis needed (keys can benumberorsymbol) - Filtering: a key remapped to
neveris dropped, by key (Exclude<K, "kind">) or by value (T[K] extends V ? K : never) - Mapping over a union of objects rather than keys, e.g. an event union to an
onClick/onKeyhandler map - Behaviour details: modifiers are still copied from
keyof T, keys that collide merge into a union, and a filteringasturns an array or tuple into an object of all its members - The run-time object still has to be built by hand with matching names
Code / implementation expected: Yes β a Getters<T> type and a filter that keeps only properties of a given value type.