Skip to content

Latest commit

 

History

History
91 lines (62 loc) · 1.41 KB

File metadata and controls

91 lines (62 loc) · 1.41 KB

typelab / utils / ObjectOverwrite

type ObjectOverwrite<Target, Source, Z> = IsNever<Source> extends true ? Target : IsNonNullish<Source> extends true ? Target : IsEqual<Source, Target> extends true ? Source : _ObjectOverwrite<Target, Source, Z>;

Overwrite properties of Target with properties of Source.

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

Type Parameters

Type Parameter Default type Description

Target

The object to be overwritten.

Source

never

The object to be assigned to the target.

Z extends _LookupType

"shallow"

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

Returns

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

Example

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

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

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