← All Exercises
Advanced Template Literal Types
intermediatetypescript
Background
Template literal types enable compile-time string pattern validation, useful for event names, CSS selectors, and API routes.
Problem
Define a type for DOM event handler names (onclick, onkeydown, etc.) using template literal types from a union of event names.
Input
A string that should match the template literal type pattern.
Output
A type-safe string or a compile-time type error.
Sample tests
Sample 1
Input: "onclick"
Expected: Valid EventHandlerName
Your solution
Learning objective
Learn advanced template literal types for compile-time string pattern validation.