Union Type to Intersection Type in TypeScript
Last Updated :
10 Jun, 2024
To Transform union type to intersection type we have different approaches. In this article, we are going to learn how to Transform union type to intersection type.
Below are the approaches used to Transform union type to intersection type:
Union Type
- A union type in TypeScript allows a variable to have one of several types. It is represented using the
|
operator. - Example:
type Animal = "Dog" | "Cat" | "Bird";
- In this example, a variable of type
Animal
can have the value "Dog", "Cat", or "Bird".
Intersection Type
- An intersection type combines multiple types into a single type, representing the combination of all types. It is represented using the
&
operator. - Example:
type Person = { name: string } & { age: number };
- In this example, a variable of type
Person
must have both a name
property of type string
and an age
property of type number
.
Using Distributive Conditional Types
Conditional types can be used in a distributive manner to distribute over a union of types, effectively creating an intersection type.
Example: In this example, the UnionToIntersection type uses distributive conditional types to transform a union type into an intersection type. The distributive conditional type operates over each member of the union individually, and the resulting type is the intersection of all the members.
JavaScript
type UnionToIntersection<U> = (
U extends any ? (k: U) => void : never
) extends (k: infer I) => void
? I
: never;
// Example usage
type UnionType = { a: number } | { b: string } | { c: boolean };
type IntersectionType = UnionToIntersection<UnionType>;
// Example object of IntersectionType
const myObject: IntersectionType = {
a: 42,
b: "hello",
c: true
};
console.log(myObject);
Output:
"a": 42,
"b": "hello",
"c": true
Using Conditional Template Literal Types
Conditional template literal types provide a versatile mechanism for manipulating and transforming types in TypeScript. By combining conditional types with template literal types, we can create an elegant solution to convert union types into intersection types.
Example: In this example, we define a type UnionToIntersection that takes a union type U and recursively distributes over each member of the union. The UnionToIntersection type leverages conditional template literal types to iteratively build an intersection type by concatenating individual members of the union.
JavaScript
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (
(k: infer I) => void) ? I : never;
// Example usage
type UnionType = { a: number } | { b: string } | { c: boolean };
type IntersectionType = UnionToIntersection<UnionType>;
const myObject: IntersectionType = {
a: 22,
b: "GFG",
c: true
};
console.log(myObject);
Output:
{
"a": 22,
"b": "GFG",
"c": true
}
Similar Reads
TypeScript Object Intersection Types In TypeScript, Object Intersection Types are a way to create new types by combining multiple types into a single type. This is done using the & (ampersand) operator. Intersection types allow you to express that an object must have all the properties and methods of each type in the intersection.
3 min read
How to Return a Union Type in TypeScript ? In TypeScript, a union type is a powerful way to express a variable that can be one of several types. Union types are used when a function or variable is expected to support multiple types of input. Union types are defined using the pipe (|) symbol between two or more types. This indicates that a va
4 min read
What are intersection types in Typescript ? In Typescript, Although intersection and union types are similar, they are employed in completely different ways. An intersection type is a type that merges several kinds into one. This allows you to combine many types to create a single type with all of the properties that you require. An object of
3 min read
How to Transform Union Type to Tuple Type in TypeScript ? In TypeScript, conversion from union type to tuple type consists of mapping each type in the union to a position in the tuple. This process consists of using mapped types or conditional types to perform the transformation. The below methods can be implemented to accomplish this task. Table of Conten
3 min read
TypeScript Extract<Type, Union> Utility Type In this article, we are going to learn about Extract<Type, Union> utility type in TypeScript, TypeScript is a strongly typed programming language that builds on JavaScript, giving you better tools at any scale. Extract<Type, Union> utility type is used to extract a subset of types from a
4 min read