[Typescript] Create a Union of Strings with All Possible Permutations of Two Unions
Here we have a Sandwich
type that's currently assigned to unknown
We also have a couple of union types, BreadType
and Filling
, that have several options as members:
type BreadType = "rye" | "brown" | "white";
type Filling = "cheese" | "ham" | "salami";
We can create a new union type with all possible permutations of two unions:
type Sandwich = `${BreadType} sandwich with ${Filling}`;
import { Equal, Expect } from "../helpers/type-utils";
type BreadType = "rye" | "brown" | "white";
type Filling = "cheese" | "ham" | "salami";
type Sandwich = `${BreadType} sandwich with ${Filling}`;
type tests = [
Expect<
Equal<
Sandwich,
| "rye sandwich with cheese"
| "rye sandwich with ham"
| "rye sandwich with salami"
| "brown sandwich with cheese"
| "brown sandwich with ham"
| "brown sandwich with salami"
| "white sandwich with cheese"
| "white sandwich with ham"
| "white sandwich with salami"
>
>
];