Template literals
In TypeScript, template literals are based on string literal types and can be expanded into multiple strings using unions. These types are useful for defining a theme name, for instance:
type Theme = 'light' | 'dark'
Theme
is a union type that can only be assigned one of the two string literal types: 'light'
or 'dark'
. This provides type safety and prevents runtime errors caused by passing an invalid value as the theme name.
Using this approach, you can define a set of possible values for a variable, argument, or parameter and ensure that only valid values are used at compile time. This makes your code more reliable and easier to maintain.