template literal type
A string literal type built with backticks, such as `on${Capitalize<K>}Change`, that combines other string types.
In more detail
Given the union "username" | "email" it produces "onUsernameChange" | "onEmailChange". Combined with key remapping it generates strongly typed event handler and getter names.
Related terms
- template literal — A JavaScript string written in backticks that can embed expressions with ${...}, such as `Hello, ${name}!`.
- 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>}`].
- 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".
- literal type — A type that allows exactly one value, such as the string "GET", the number 404 or true.
Taught in
- Conditional Types: Ternary Types And Type Inference With infer — Production TypeScript
- Mapped Types, Key Remapping With as And Template Literals — Production TypeScript