Why isn't this code typechking with flow?
为什么这个代码没有流量类型?
function runFunction<I, O>( fun: (...args:Array<I>) => O, args:Array<I> ): O {
var res = fun.apply(null, args);
return res;
}
function myFun(first: number, second: number): string {
return first + ", " + second;
}
runFunction(myFun, [1, 2]);
I think I am doing everything right, and I still get this
我想我做的一切都是正确的,我仍然得到这个
function type
Too few arguments (expected default/rest parameters in function)
function
rest array of type parameter I of function call
This type is incompatible with
number
1 个解决方案
#1
Oh now I get it. I would have to do this
哦,现在我明白了。我必须这样做
function runFunction<I, O>( fun: (...args:Array<I>) => O, args:Array<I> ): O {
var res = fun.apply(null, args);
return res;
}
function myFun(...args:Array<number>): string {
return args[0] + ", " + args[1];
}
runFunction(myFun, [1, 2]);
So, if I get it correctly, "function that takes 2 numbers" is not a subtype of "function that takes any amount of any parameters", even when it logically is a subtype.
因此,如果我正确地得到它,“带2个数字的函数”不是“接受任何数量的任何参数的函数”的子类型,即使它在逻辑上是一个子类型。
I am not sure what would be the right solution... I understand, kind of, the logic, but it's confusing slightly.
我不确定什么是正确的解决方案...我理解,有点,逻辑,但它有点令人困惑。
#1
Oh now I get it. I would have to do this
哦,现在我明白了。我必须这样做
function runFunction<I, O>( fun: (...args:Array<I>) => O, args:Array<I> ): O {
var res = fun.apply(null, args);
return res;
}
function myFun(...args:Array<number>): string {
return args[0] + ", " + args[1];
}
runFunction(myFun, [1, 2]);
So, if I get it correctly, "function that takes 2 numbers" is not a subtype of "function that takes any amount of any parameters", even when it logically is a subtype.
因此,如果我正确地得到它,“带2个数字的函数”不是“接受任何数量的任何参数的函数”的子类型,即使它在逻辑上是一个子类型。
I am not sure what would be the right solution... I understand, kind of, the logic, but it's confusing slightly.
我不确定什么是正确的解决方案...我理解,有点,逻辑,但它有点令人困惑。