Make _.curry and _.curryRight more typed

This commit is contained in:
tkqubo
2015-10-11 16:18:32 +09:00
parent 14ab703f43
commit 734408e70a
2 changed files with 167 additions and 17 deletions

View File

@@ -1988,23 +1988,53 @@ result = <_.LoDashObjectWrapper<() => boolean>>_(createCallbackObj).createCallba
// _.curry
var testCurryFn = (a: number, b: number, c: number) => [a, b, c];
interface TestCurryResultFn {
(...args: number[]): number[] | TestCurryResultFn;
}
result = <number[]>_.curry<TestCurryResultFn>(testCurryFn)(1, 2, 3);
result = <TestCurryResultFn>_.curry<TestCurryResultFn>(testCurryFn)(1);
result = <number[]>_(testCurryFn).curry<TestCurryResultFn>().value()(1, 2, 3);
result = <TestCurryResultFn>_(testCurryFn).curry<TestCurryResultFn>().value()(1);
let curryResult0: number[]
let curryResult1: _.CurriedFunction1<number, number[]>
let curryResult2: _.CurriedFunction2<number, number, number[]>
curryResult0 = _.curry(testCurryFn)(1, 2, 3);
curryResult1 = _.curry(testCurryFn)(1, 2);
curryResult0 = _.curry(testCurryFn)(1, 2)(3);
curryResult0 = _.curry(testCurryFn)(1)(2)(3);
curryResult2 = _.curry(testCurryFn)(1);
curryResult1 = _.curry(testCurryFn)(1)(2);
curryResult0 = _.curry(testCurryFn)(1)(2)(3);
curryResult0 = _.curry(testCurryFn)(1)(2, 3);
curryResult0 = _(testCurryFn).curry().value()(1, 2, 3);
curryResult2 = _(testCurryFn).curry().value()(1);
declare function testCurry2(a: string, b: number, c: boolean): [string, number, boolean];
let curryResult3: [string, number, boolean];
let curryResult4: _.CurriedFunction1<boolean, [string, number, boolean]>;
let curryResult5: _.CurriedFunction2<number, boolean, [string, number, boolean]>;
let curryResult6: _.CurriedFunction3<string, number, boolean, [string, number, boolean]>;
curryResult3 = _.curry(testCurry2)("1", 2, true);
curryResult3 = _.curry(testCurry2)("1", 2)(true);
curryResult3 = _.curry(testCurry2)("1")(2, true);
curryResult3 = _.curry(testCurry2)("1")(2)(true);
curryResult4 = _.curry(testCurry2)("1", 2);
curryResult4 = _.curry(testCurry2)("1")(2);
curryResult5 = _.curry(testCurry2)("1");
curryResult6 = _.curry(testCurry2);
// _.curryRight
var testCurryRightFn = (a: number, b: number, c: number) => [a, b, c];
interface TestCurryRightResultFn {
(...args: number[]): number[] | TestCurryRightResultFn;
}
result = <number[]>_.curryRight<TestCurryRightResultFn>(testCurryRightFn)(1, 2, 3);
result = <TestCurryRightResultFn>_.curryRight<TestCurryRightResultFn>(testCurryRightFn)(1);
result = <number[]>_(testCurryRightFn).curryRight<TestCurryRightResultFn>().value()(1, 2, 3);
result = <TestCurryRightResultFn>_(testCurryRightFn).curryRight<TestCurryRightResultFn>().value()(1);
curryResult0 = _.curryRight(testCurryRightFn)(1, 2, 3);
curryResult2 = _.curryRight(testCurryRightFn)(1);
curryResult0 = _(testCurryRightFn).curryRight().value()(1, 2, 3);
curryResult2 = _(testCurryRightFn).curryRight().value()(1);
let curryResult7: _.CurriedFunction1<string, [string, number, boolean]>;
let curryResult8: _.CurriedFunction2<number, string, [string, number, boolean]>;
let curryResult9: _.CurriedFunction3<boolean, number, string, [string, number, boolean]>;
curryResult3 = _.curryRight(testCurry2)(true, 2, "1");
curryResult3 = _.curryRight(testCurry2)(true, 2)("1");
curryResult3 = _.curryRight(testCurry2)(true)(2, "1");
curryResult3 = _.curryRight(testCurry2)(true)(2)("1");
curryResult7 = _.curryRight(testCurry2)(true, 2);
curryResult7 = _.curryRight(testCurry2)(true)(2);
curryResult8 = _.curryRight(testCurry2)(true);
curryResult9 = _.curryRight(testCurry2);
declare var source: any;
result = <Function>_.debounce(function () { }, 150);

126
lodash/lodash.d.ts vendored
View File

@@ -3585,7 +3585,7 @@ declare module _ {
predicate?: TObject
): TResult;
}
//_.findWhere
interface LoDashStatic {
/**
@@ -6184,7 +6184,87 @@ declare module _ {
*/
curry<TResult extends Function>(
func: Function,
arity?: number): TResult;
arity: number): TResult;
/**
* Creates a function that accepts one or more arguments of func that when called either invokes func returning
* its result, if all func arguments have been provided, or returns a function that accepts one or more of the
* remaining func arguments, and so on. The arity of func may be specified if func.length is not sufficient.
* @param func The function to curry.
* @return Returns the new curried function.
*/
curry<T1, R>(func: (t1: T1) => R):
CurriedFunction1<T1, R>;
/**
* Creates a function that accepts one or more arguments of func that when called either invokes func returning
* its result, if all func arguments have been provided, or returns a function that accepts one or more of the
* remaining func arguments, and so on. The arity of func may be specified if func.length is not sufficient.
* @param func The function to curry.
* @return Returns the new curried function.
*/
curry<T1, T2, R>(func: (t1: T1, t2: T2) => R):
CurriedFunction2<T1, T2, R>;
/**
* Creates a function that accepts one or more arguments of func that when called either invokes func returning
* its result, if all func arguments have been provided, or returns a function that accepts one or more of the
* remaining func arguments, and so on. The arity of func may be specified if func.length is not sufficient.
* @param func The function to curry.
* @return Returns the new curried function.
*/
curry<T1, T2, T3, R>(func: (t1: T1, t2: T2, t3: T3) => R):
CurriedFunction3<T1, T2, T3, R>;
/**
* Creates a function that accepts one or more arguments of func that when called either invokes func returning
* its result, if all func arguments have been provided, or returns a function that accepts one or more of the
* remaining func arguments, and so on. The arity of func may be specified if func.length is not sufficient.
* @param func The function to curry.
* @return Returns the new curried function.
*/
curry<T1, T2, T3, T4, R>(func: (t1: T1, t2: T2, t3: T3, t4: T4) => R):
CurriedFunction4<T1, T2, T3, T4, R>;
/**
* Creates a function that accepts one or more arguments of func that when called either invokes func returning
* its result, if all func arguments have been provided, or returns a function that accepts one or more of the
* remaining func arguments, and so on. The arity of func may be specified if func.length is not sufficient.
* @param func The function to curry.
* @return Returns the new curried function.
*/
curry<T1, T2, T3, T4, T5, R>(func: (t1: T1, t2: T2, t3: T3, t4: T4, t5: T5) => R):
CurriedFunction5<T1, T2, T3, T4, T5, R>;
}
interface CurriedFunction1<T1, R> {
(): CurriedFunction1<T1, R>;
(t1: T1): R;
}
interface CurriedFunction2<T1, T2, R> {
(): CurriedFunction2<T1, T2, R>;
(t1: T1): CurriedFunction1<T2, R>;
(t1: T1, t2: T2): R;
}
interface CurriedFunction3<T1, T2, T3, R> {
(): CurriedFunction3<T1, T2, T3, R>;
(t1: T1): CurriedFunction2<T2, T3, R>;
(t1: T1, t2: T2): CurriedFunction1<T3, R>;
(t1: T1, t2: T2, t3: T3): R;
}
interface CurriedFunction4<T1, T2, T3, T4, R> {
(): CurriedFunction4<T1, T2, T3, T4, R>;
(t1: T1): CurriedFunction3<T2, T3, T4, R>;
(t1: T1, t2: T2): CurriedFunction2<T3, T4, R>;
(t1: T1, t2: T2, t3: T3): CurriedFunction1<T4, R>;
(t1: T1, t2: T2, t3: T3, t4: T4): R;
}
interface CurriedFunction5<T1, T2, T3, T4, T5, R> {
(): CurriedFunction5<T1, T2, T3, T4, T5, R>;
(t1: T1): CurriedFunction4<T2, T3, T4, T5, R>;
(t1: T1, t2: T2): CurriedFunction3<T3, T4, T5, R>;
(t1: T1, t2: T2, t3: T3): CurriedFunction2<T4, T5, R>;
(t1: T1, t2: T2, t3: T3, t4: T4): CurriedFunction1<T5, R>;
(t1: T1, t2: T2, t3: T3, t4: T4, t5: T5): R;
}
interface LoDashObjectWrapper<T> {
@@ -6205,7 +6285,47 @@ declare module _ {
*/
curryRight<TResult extends Function>(
func: Function,
arity?: number): TResult;
arity: number): TResult;
/**
* This method is like _.curry except that arguments are applied to func in the manner of _.partialRight
* instead of _.partial.
* @param func The function to curry.
* @return Returns the new curried function.
*/
curryRight<T1, R>(func: (t1: T1) => R):
CurriedFunction1<T1, R>;
/**
* This method is like _.curry except that arguments are applied to func in the manner of _.partialRight
* instead of _.partial.
* @param func The function to curry.
* @return Returns the new curried function.
*/
curryRight<T1, T2, R>(func: (t1: T1, t2: T2) => R):
CurriedFunction2<T2, T1, R>;
/**
* This method is like _.curry except that arguments are applied to func in the manner of _.partialRight
* instead of _.partial.
* @param func The function to curry.
* @return Returns the new curried function.
*/
curryRight<T1, T2, T3, R>(func: (t1: T1, t2: T2, t3: T3) => R):
CurriedFunction3<T3, T2, T1, R>;
/**
* This method is like _.curry except that arguments are applied to func in the manner of _.partialRight
* instead of _.partial.
* @param func The function to curry.
* @return Returns the new curried function.
*/
curryRight<T1, T2, T3, T4, R>(func: (t1: T1, t2: T2, t3: T3, t4: T4) => R):
CurriedFunction4<T4, T3, T2, T1, R>;
/**
* This method is like _.curry except that arguments are applied to func in the manner of _.partialRight
* instead of _.partial.
* @param func The function to curry.
* @return Returns the new curried function.
*/
curryRight<T1, T2, T3, T4, T5, R>(func: (t1: T1, t2: T2, t3: T3, t4: T4, t5: T5) => R):
CurriedFunction5<T5, T4, T3, T2, T1, R>;
}
interface LoDashObjectWrapper<T> {