FlareStart
HomeNewsHow ToSources
FlareStart

Where developers start their day. All the tech news & tutorials that matter, in one place.

Quick Links

  • Home
  • News
  • Tutorials
  • Sources
  • Privacy Policy

Connect

© 2026 FlareStart. All rights reserved.

Back to articles
Mastering Union and Intersection Types in TypeScript: Quick Guide with Examples
How-ToWeb Development

Mastering Union and Intersection Types in TypeScript: Quick Guide with Examples

via Dev.toJeferson Eiji2h ago

Union Types: Use union types when a variable or parameter can hold values of multiple types. Syntax: typeA | typeB Example: let value : string | number ; value = " hello " ; // valid value = 42 ; // valid value = true ; // invalid Intersection Types: Use intersection types to combine multiple types into one. The resulting type has all properties from each type. Syntax: typeA & typeB Example: type A = { a : number }; type B = { b : string }; let obj : A & B = { a : 1 , b : " hello " }; Key Differences: Union ( | ) : Accepts values that match any member type. Intersection ( & ) : Requires values to satisfy all member types. Practical Example: Suppose you have user types: type Admin = { name : string ; admin : true }; type User = { name : string ; admin ?: false }; type AnyUser = Admin | User ; // Can be either Admin or User function greet ( user : AnyUser ) { console . log ( `Hello, ${ user . name } ` ); } When to Use Them: Use unions when data can be of different shapes. Use intersectio

Continue reading on Dev.to

Opens in a new tab

Read Full Article
0 views

Related Articles

You Don’t Need More Tutorials - You Need Better Problems
How-To

You Don’t Need More Tutorials - You Need Better Problems

Medium Programming • 23m ago

Autonomous agents are easy to build. Secure authorization is the hard part.
How-To

Autonomous agents are easy to build. Secure authorization is the hard part.

Medium Programming • 2h ago

This free privacy tool makes it super easy to see which sites are selling your data
How-To

This free privacy tool makes it super easy to see which sites are selling your data

ZDNet • 3h ago

Oupes Mega 1 review: I finally found a portable power station I can store in my truck
How-To

Oupes Mega 1 review: I finally found a portable power station I can store in my truck

ZDNet • 4h ago

I Recreated a $200 TradingView Indicator in Pine Script for Free — Here’s How
How-To

I Recreated a $200 TradingView Indicator in Pine Script for Free — Here’s How

Medium Programming • 4h ago

Discover More Articles