profileRyan KesPGP keyI build stuffEmailGithubTwitterLast.fmMastodonMatrix

TypeScript Intersection Type

Description

extend is a very common pattern in JavaScript where you take two objects and create a new one that has the features of both these objects. An Intersection Type allows you to use this pattern in a safe way.

Syntax

function extend<T, U>(first: T, second: U): T & U {
  return { ...first, ...second }
}

const x = extend({ a: "hello" }, { b: 42 })

// x now has both `a` and `b`
const a = x.a
const b = x.b