Skip to content

Latest commit

 

History

History
42 lines (33 loc) · 1.09 KB

File metadata and controls

42 lines (33 loc) · 1.09 KB

Home > types-kit > Merge

Merge type

Merge two types into a new type. Keys of the second type will override keys of the first type.

Signature:

export type Merge<A, B> = A extends readonly unknown[]
  ? B extends readonly unknown[]
    ? MergeTuple<A, B>
    : Simplify<
        StrictOmit<A, Extract<Keys<A>, Keys<B>>> & {
          [P in keyof B as P extends Keys<B> ? P : never]: B[P]
        }
      >
  : Simplify<
      StrictOmit<A, Extract<Keys<A>, Keys<B>>> & {
        [P in keyof B as P extends Keys<B> ? P : never]: B[P]
      }
    >

References: MergeTuple, Simplify, StrictOmit, Keys

Example

interface Foo {
    a: number;
    b: string;
};
interface Bar {
  b: number;
};
// Expect: { a: number, b: number }
type NewProps = Merge<Foo, Bar>