Skip to content

Instantly share code, notes, and snippets.

@ArtemAvramenko
Created March 22, 2016 18:02
Show Gist options
  • Save ArtemAvramenko/088b4c620318a3e13411 to your computer and use it in GitHub Desktop.
Save ArtemAvramenko/088b4c620318a3e13411 to your computer and use it in GitHub Desktop.
declare interface MaybeFunction {
<T1>(a: T1): T1;
<T1, T2>(a: T1, b: (_: T1) => T2): T2;
<T1, T2, T3>(a: T1, b: (_: T1) => T2, c: (_: T2) => T3): T3;
<T1, T2, T3, T4>(a: T1, b: (_: T1) => T2, c: (_: T2) => T3, d: (_: T3) => T4): T4;
}
var maybe = <MaybeFunction>function() {
let result = arguments[0];
for (let i = 1, len = arguments.length; i < len; i++) {
if (result == null) {
return;
}
result = arguments[i](result);
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment