4 ms·
If I understand your pipe function correctly - it takes an init value and a series of functions that map a type T to the same type T - then this should do it.
by xsmasher 6y ago
If I understand your pipe function correctly - it takes an init value and a series of functions that map a type T to the same type T - then this should do it.
function pipe<T>(init: T, ...ops: ((x: T) => T)[]): T{
let r:T = ops.reduce((acc:T, op) => op(acc), init);
return r;
}
I had 5 years experience fixing type errors in C++; typescript seems like a dream in comparison.
Edit: I just realized your operations probably DON'T return the same type that they are passed; they probably work in a chain like (t:T) => U, then (u:U) => V, etc. Nevermind!
- runarberg 6y agoYup, the annotation should be something like: type Ops<T, R, N> = [ (x: T) => N[0], ...<n>((x: N[n]) => N[n + 1]), (x: N.last) => R, ]; function pipe<T>(init: T): T; function pipe<R, T, Ops>(init: T, ...ops: ...Ops<T, R>): R;