tsafe
GitHub
  • 👋What is tsafe?
  • ⚙️How to import
  • assert
  • Equals
  • id
  • is
  • objectKeys
  • exclude
  • isAmong
  • symToStr
  • ReturnType
  • Parameters
  • Param0
  • typeGuard
  • capitalize/uncapitalize
  • MethodNames
  • isPromiseLike
  • flip
  • objectEntries
  • objectFromEntries
  • UnionToIntersection
  • 🚧withDefaults
  • 📉UnpackPromise
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub

UnionToIntersection


type A = { foo: string; };
type B = { bar: number; };

type Got = UnionToIntersection<A | B>;
type Expected = A & B;

assert<Equals<Got,Expected>>();

Example:

import type { UnionToIntersection } from "tsafe";

const o1= {
  "p1": { "a": "foo" },
  "p2": { "b": "foo", "c": "foo" }
};

const o2: UnionToIntersection<(typeof o1)[keyof typeof o1]> = {} as any;

objectKeys(o1).forEach(key=> Object.assign(o2, o1[key]));

//o2 is of type { a: string; b: string; c: string; }
PreviousobjectFromEntriesNextwithDefaults

Last updated 6 months ago

Was this helpful?

Credit goes to for this type, .

jcalz
see SO answer