JuleZ
JuleZ

Reputation: 276

Concatenate two TypeScript type as one

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'

Upvotes: 2

Views: 556

Answers (1)

AKX
AKX

Reputation: 168843

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.

Upvotes: 5

Related Questions