Concatenate two TypeScript type as one

Viewed 77

I would like to know if there is a way to concatenate two typescript types as one example :

type Size = 'xl' | 'xxl' ...;
let textSize: 'text-' & Size;
// So my type is something like : 'text-xl' | 'text-xxl'
1 Answers

By using a template literal type.

type Size = 'xl' | 'xxl';
type TextSizeType = `text-${Size}`;

TS resolves TextSizeType to

type TextSizeType = "text-xl" | "text-xxl"

which sounds like what you want.

Related