TypeScript 4.3: Template String Types

Template Literal Types are amazing for typing strings with patterns.

type Color = "red" | "blue";
type Quantity = "one" | "two";

type Item = `${Color}-${Quantity}`;
// Type is "red-one" | "red-two" | "blue-one" | "blue-two"

This is incredibly useful for typing CSS classes, event names, or any string-based API contract.


Discover more from C4: Container, Code, Cloud & Context

Subscribe to get the latest posts sent to your email.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.