Question presented to candidate: "What are template literal types in TypeScript? Show me a couple of places you would actually use them, and how you would pull information back out of a string type."
What a strong answer should cover:
- The syntax mirrors JavaScript template strings, but builds string literal types:
`on${Capitalize<Ev>}`; added in TypeScript 4.1 - Unions in the holes multiply: every combination is produced, and very large products are refused (error TS2590)
- The intrinsic helpers
Uppercase,Lowercase,Capitalize,Uncapitalize, and primitive holes such as`${number}px`that make patterns rather than finite unions - Combined with key remapping (
as) to derive property names, e.g.onNameChangehandlers from a model - With
inferin a conditional type they parse strings: route params,Split, dotted paths; a leadinginfertakes the shortest match
Code / implementation expected: Yes β derive event or handler names, and write one small parser such as route params.