JS Built-ins
TypeScriptintermediate

Interface β€” Intersection Types

πŸ“– Usage Example
type AB = { a: number } & { b: string }
const v: AB = { a: 1, b: 'hello' }  // must have both a and b

An intersection type (A & B) requires an object to satisfy all constituent types simultaneously.

Intersection Types (&)

An intersection type A & B combines all properties of both types.

Challenge: Implement mergeObjects<T, U>(a, b) returning T & U.

mergeObjects({ a: 1 }, { b: 2 })  // β†’ { a: 1, b: 2 }
πŸ’‘ Hints (1)
  • β€’ Object.assign({}, a, b) spreads both objects into a fresh one.