Home > database >  How to merge / combine / extend type declarations
How to merge / combine / extend type declarations

Time:02-07

In typescript, how can I create a new type from multiple type declarations in an outer join kind of way ?

I have 2 type declarations

type OverflowPosition = "safe" | "unsafe" | "";
type BaselinePosition = "baseline" | "center";

I would like to see them combined to

type OverflowBaselinePosition = "safe baseline" | "safe center" | "unsafe baseline" | "unsafe center" | "baseline" | "center" ;

CodePudding user response:

You can use template literal types, e.g.

type OverflowPosition = "safe" | "unsafe" | "";
type BaselinePosition = "baseline" | "center";

type CombinedPosition = `${OverflowPosition} ${BaselinePosition}`

Playground link

  •  Tags:  
  • Related