Skip to content

Latest commit

 

History

History
93 lines (63 loc) · 1.69 KB

File metadata and controls

93 lines (63 loc) · 1.69 KB

typelab / utils / FunctionOverwrite

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

Overwrite the parameter and return types of Target with parameter and return types of Source.

Use `ArrayOverwrite` for the parameter type and `ObjectOverwrite` 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 type.

Source extends Fn

The source Function type.

Z extends _LookupType

"shallow"

Returns

A new Function with overwritten parameter and return types.

Example

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

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

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