curried 定义在 uncurried 定义之后时出现 Typescript 错误
Typescript error when curried defition is after the uncurried definition
我收到错误消息:Expected 1 arguments, but got 2.ts(2554)
当我将方法的未柯里化定义放在柯里化定义之上时。
未通过 dtslint
测试:
function match(regExpression: RegExp, str: string): string[];
function match(regExpression: RegExp): (str: string) => string[];
function throttle<T, U>(fn: (input: T) => U, ms: number): (input: T) => U;
function throttle<T, Q, U>(
fn: (input1: T, input2: Q) => U, ms: number
): (input1: T, input2: Q) => U;
it('match', () => {
const fn = throttle(match, 1000)
fn(/foo/,'foo bar') // line of error
})
如果我将柯里化 match
定义移动到非柯里化定义上方,则错误消失。
我的问题是我真的应该将未柯里化版本放在柯里化版本之后,还是 throttle
定义有误?
问题的上下文是为类似于 ramda
的库编写 Typescript 定义。
Typescript 按从上到下的顺序检查重载的签名(内部交集类型)。当 throttle
选择使用哪个签名来推断泛型参数(几乎与条件类型相同)时,它总是选择最后一个签名。
描述了与 TS 类型系统密切相关的特性 here:
When inferring from a type with multiple call signatures (such as the type of an overloaded function), inferences are made from the last signature (which, presumably, is the most permissive catch-all case). It is not possible to perform overload resolution based on a list of argument types (this would require us to support typeof for arbitrary expressions, as suggested in #6606, or something similar).
我收到错误消息:Expected 1 arguments, but got 2.ts(2554)
当我将方法的未柯里化定义放在柯里化定义之上时。
未通过 dtslint
测试:
function match(regExpression: RegExp, str: string): string[];
function match(regExpression: RegExp): (str: string) => string[];
function throttle<T, U>(fn: (input: T) => U, ms: number): (input: T) => U;
function throttle<T, Q, U>(
fn: (input1: T, input2: Q) => U, ms: number
): (input1: T, input2: Q) => U;
it('match', () => {
const fn = throttle(match, 1000)
fn(/foo/,'foo bar') // line of error
})
如果我将柯里化 match
定义移动到非柯里化定义上方,则错误消失。
我的问题是我真的应该将未柯里化版本放在柯里化版本之后,还是 throttle
定义有误?
问题的上下文是为类似于 ramda
的库编写 Typescript 定义。
Typescript 按从上到下的顺序检查重载的签名(内部交集类型)。当 throttle
选择使用哪个签名来推断泛型参数(几乎与条件类型相同)时,它总是选择最后一个签名。
描述了与 TS 类型系统密切相关的特性 here:
When inferring from a type with multiple call signatures (such as the type of an overloaded function), inferences are made from the last signature (which, presumably, is the most permissive catch-all case). It is not possible to perform overload resolution based on a list of argument types (this would require us to support typeof for arbitrary expressions, as suggested in #6606, or something similar).