Skip to content

Latest commit

 

History

History
93 lines (63 loc) · 1.66 KB

File metadata and controls

93 lines (63 loc) · 1.66 KB

typelab / utils / FunctionMerge

type FunctionMerge<Target, Source, Z> = [Target, Source] extends [Fn<infer TargetParams, infer TargetReturn>, Fn<infer SourceParams, infer SourceReturn>] ? (...param) => ObjectMerge<TargetReturn, SourceReturn, Z> : never;

Merges the parameter and return types of Source into Target.

Use `ArrayMerge` for the parameter type and `ObjectMerge` for the return type.

  • If Target or Source is not Function, it returns never.

Type Parameters

Type Parameter Default type Description

Target extends Fn

The target Function.

Source extends Fn

The source Function to merge from.

Z extends _LookupType

"shallow"

Returns

A new Function combining parameter and return types from both Target and Source.

Example

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

// (param_0: string | number, param_1: string) => { a: string | number; b: { a: string; } | { b: number; }; }
type Shallow = FunctionMerge<(...param: [string, string]) => Obj1, (...param: [number]) => Obj2, 'shallow'>;

// (param_0: string | number, param_1: string) => { a: string | number; b: { a: string; b: number; }; }
type Deep = FunctionMerge<(...param: [string, string]) => Obj1, (...param: [number]) => Obj2, 'deep'>;