TypeScript 4.3: Template String Types

TypeScript 4.3 turns the type system into a programming language itself.

Template Literal Types

You can now concatenate types!

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

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

This is incredibly useful for typing strings that follow a specific pattern, like CSS classes or Event names.


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.