Visit complete TypeScript roadmap
TypeScript Topic

Combining Types

Combining Types

In TypeScript, you can combine types using type union and type intersection.

Type Union:

The union operator | is used to combine two or more types into a single type that represents all the possible types. For example:

type stringOrNumber = string | number;
let value: stringOrNumber = 'hello';

value = 42;

Type Intersection:

The intersection operator & is used to intersect two or more types into a single type that represents the properties of all the types. For example:

interface A {
  a: string;
}

interface B {
  b: number;
}

type AB = A & B;
let value: AB = { a: 'hello', b: 42 };

Learn more from the following links:

More Topics

Explore related content

View All Topics
Loved by 100K+ Developers

Start Your Learning
Journey Today

Join thousands of developers who are leveling up their skills with structured roadmaps and expert guidance

No credit card required
Always free
Track your progress