Skip to content

Commit

Permalink
fix: fixes pipe types
Browse files Browse the repository at this point in the history
  • Loading branch information
OctoD committed Mar 11, 2021
1 parent 2f22aa6 commit 9393122
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
9 changes: 5 additions & 4 deletions src/pipe-async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ type Tail<T extends any[]> = ((...t: T) => void) extends (
: never;

type PipeAsync = <
F extends [(arg: any) => Promise<any>, ...Array<(arg: any) => Promise<any>>]
F extends [((arg: any) => Promise<any> | (() => Promise<any>)), ...Array<(arg: any) => Promise<any>>]
>(
...f: F & AsChain<F>
) => (arg: ArgType<F[0]>) => Promise<ReturnType<F[LastIndexOf<F>]>>;
) =>
F[0] extends () => any ? () => ReturnType<F[LastIndexOf<F>]> : F[0] extends (arg: infer U) => any ? (arg: U) => ReturnType<F[LastIndexOf<F>]> : never;

/**
* Creates a pipeline of asynchronous functions.
Expand Down Expand Up @@ -73,7 +74,7 @@ type PipeAsync = <
*/
export const pipeasync: PipeAsync = (...args) => {
const [first, ...othersFns] = args;
return async (arg) => {
return (async (arg: any) => {
const firstvalue = await first(arg);
let latestvalue = firstvalue;

Expand All @@ -82,5 +83,5 @@ export const pipeasync: PipeAsync = (...args) => {
}

return latestvalue;
};
}) as any;
};
9 changes: 6 additions & 3 deletions src/pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ type Tail<T extends any[]> = ((...t: T) => void) extends (
? U
: never;

type Pipe = <F extends [(arg: any) => any, ...Array<(arg: any) => any>]>(
type Pipe = <F extends [((arg: any) => any | (() => any)), ...Array<(arg: any) => any>]>(
...f: F & AsChain<F>
) => (arg: ArgType<F[0]>) => ReturnType<F[LastIndexOf<F>]>;
) =>
F[0] extends () => any ? () => ReturnType<F[LastIndexOf<F>]> : F[0] extends (arg: infer U) => any ? (arg: U) => ReturnType<F[LastIndexOf<F>]> : never;

// (arg: ArgType<F[0]>) => ReturnType<F[LastIndexOf<F>]>;

/**
* Creates a pipeline of synchronous functions
Expand All @@ -48,5 +51,5 @@ type Pipe = <F extends [(arg: any) => any, ...Array<(arg: any) => any>]>(
*/
export const pipe: Pipe = (...args) => {
const [first, ...othersFns] = args;
return (arg) => othersFns.reduce((retval, fn) => fn(retval), first(arg));
return ((arg: unknown) => othersFns.reduce((retval, fn) => fn(retval), first(arg))) as any;
};

0 comments on commit 9393122

Please sign in to comment.