type TypeOne = { one: string };
type TypeTwo = { two: number };
type TypeThree = { three: string };
type TypeNums = { one: TypeOne; two: TypeTwo; three: TypeThree };
const numNames = ['one', 'two'] as const;
type TypeNumNames = typeof numNames;
// Same as `TypeOne | TypeTwo`
// `{ one: string } | { two: number }`
type TypeOneTwoUnion = TypeNums[TypeNumNames[number]];
// Same as `TypeOne & TypeTwo`
// `{ one: string; two: number }`
type TypeOneTwoIntersection = UnionToIntersection<TypeOneTwoUnion>;
Converts a union type to an intersection type (
|
to&
).Note: this utility type is for use on object types. It may fail on other types.