Skip to content

Latest commit

 

History

History
91 lines (62 loc) · 1.8 KB

File metadata and controls

91 lines (62 loc) · 1.8 KB

typelab / utils / ObjectMerge

type ObjectMerge<Target, Source, Z> = IsNever<Source> extends true ? Target : IsNonNullish<Source> extends true ? Target : IsObjectEmpty<Source> extends true ? Target : IsEqual<Source, Target> extends true ? Source : [IsObjectLiteral<Target>, IsObjectLiteral<Source>] extends [true, true] ? ObjectPartial<{ [K in keyof Target | keyof Source]: K extends keyof Target & keyof Source ? _Lookup<Z, { shallow: (...) | (...); deep: ObjectMerge<(...), (...)> }> : K extends keyof Target ? Target[K] : K extends keyof (...) ? (...)[(...)] : never }, ObjectOptionalKeys<Source | Target>> : Target | Source;

Merges properties of Target and Source.

This type will merge all nested object types (if Z is 'deep').

Type Parameters

Type Parameter Default type Description

Target

The first object type.

Source

never

The second object type to merge into the first.

Z extends _LookupType

"shallow"

Defines the lookup type, which can be 'deep' or 'shallow', defaults to 'shallow'.

Returns

A new object type that merges the properties of both Target and Source.

Example

type Obj1 = { a: string; b: string; c: { a: string; } };
type Obj2 = { b: number; c: { a: number; b: number }; d: number };

// { a: string; b: string | number; c: { a: string; } | { a: number; b: number }; d: number }
type Shallow = ObjectMerge<Obj1, Obj2, 'shallow'>;

// { a: string; b: string | number; c: { a: string | number; b: number; }; d: number }
type Deep = ObjectMerge<Obj1, Obj2, 'deep'>;