Home > Mobile >  Concatenate two TypeScript type as one
Concatenate two TypeScript type as one

Time:01-10

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'

CodePudding user response:

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.

  •  Tags:  
  • Related