Typescript 4.1

Quick Tip: Template Literal Types in Typescript 4.1

We all know that Typescript supports custom union types that only allows specific values to the variables of that type, like below:

type mood ='😀' | '😔' | '😡' | '😭' | '😢' | '💔';

type intensity = '1x' | '2x' | '5x' | '10x' | '100x';

But what if we want to have a combined type that unions all possible combination of above two types?

Thanks to Template Literal Type features in Typescript 4.1, which allows us to combine multiple types using string interpolation like below:

type intenseMood = `${intensity}-${mood}`;

That gives us a new type will all possible values like below:

3 thoughts on “Quick Tip: Template Literal Types in Typescript 4.1”

Leave a Comment

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