Skip to content

Instantly share code, notes, and snippets.

@kimjoar
Created January 21, 2018 00:23
Show Gist options
  • Save kimjoar/e8821451276b45867c0220c44b7bcf86 to your computer and use it in GitHub Desktop.
Save kimjoar/e8821451276b45867c0220c44b7bcf86 to your computer and use it in GitHub Desktop.
type Fn<T, R> = (x: T) => R;
declare function pipeArgs<T, A, B, C>(source: T, op1: Fn<T, A>, op2: Fn<A, B>, op3: Fn<B, C>): C;
declare function pipeArgs<T, A, B>(source: T, op1: Fn<T, A>, op2: Fn<A, B>): B;
declare function pipeArgs<T, A>(source: T, op1: Fn<T, A>): A;
declare function pipeArray<T, A, B, C>(source: T, ops: [Fn<T, A>, Fn<A, B>, Fn<B, C>]): C;
declare function pipeArray<T, A, B>(source: T, ops: [Fn<T, A>, Fn<A, B>]): B;
declare function pipeArray<T, A>(source: T, ops: [Fn<T, A>]): A;
const filter = <T>(predicate: (item: T) => boolean) => (items: T[]) => items.filter(predicate);
const first = <T>() => (items: T[]) => items[0];
// Both of these work as expected
const args1: number = pipeArgs([1, 2, 3], first());
const args2: number = pipeArgs([1, 2, 3], filter(x => x > 1), first());
// ... but when we use the array version, the second example fails:
const array1: number = pipeArray([1, 2, 3], [first()]);
const array2: number = pipeArray([1, 2, 3], [filter(x => x > 1), first()]);
// fails with `Type '{}' is not assignable to type 'number'.`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment