key remapping
Renaming or filtering keys inside a mapped type with an as clause, such as [K in keyof T as `get${Capitalize<K & string>}`].
In more detail
Mapping a key to never removes it from the result. Together with template literal types it generates names such as onUsernameChange from username. It was added in TypeScript 4.1.
Related terms
- mapped type — A type that builds a new object type by looping over a set of keys, such as { [K in keyof T]: T[K] }.
- template literal type — A string literal type built with backticks, such as `on${Capitalize<K>}Change`, that combines other string types.
- Capitalize<S> — A built-in string utility type that turns the first letter of a string literal type into a capital, so "email" becomes "Email".
- never (type) — The TypeScript type with no values, used for code that can never be reached or functions that never return.
Taught in
- Conditional Types: Ternary Types And Type Inference With infer — Production TypeScript
- Mapped Types, Key Remapping With as And Template Literals — Production TypeScript