我可以使用通用界面:
CurriedFunction3<T1, T2, T3, R>: R
我想创建一个填充其中一些类型参数的const绑定。像这样:
type CurriedSorter<T> = CurriedFunction3<(obj: T) => any, string, T[], T[]>;
但是,当我尝试为此类型分配const
绑定时,出现错误:
type CurriedSorter<T> = CurriedFunction3<(obj: T) => any, string, T[], T[]>;
const sortFactory: CurriedSorter = uncurryN(3, flip(compose(
unapply(sortWith),
uncurryN(2, ifElse(
compose(equals('DESC'), toUpper),
always(descend),
always(ascend),
)),
)));
通用类型
CurriedSorter
需要1个类型的参数。
const
绑定sortFactory
应该是一个具有一个泛型类型参数的函数,可以这样使用:
sortFactory<MyType>(
prop('name'),
'DESC',
[{ name: 'foo' }, { name: 'bar' }]
) // returns `[{ name: 'bar' }, { name: 'foo' }]`
如何在TypeScript中一般输入变量绑定?有没有办法用TypeScript做到这一点?
答案 0 :(得分:3)
您不能在变量声明的类型中使用T
,因为T
可能会通过属性泄漏出值。但是,您可以将通用调用签名作为变量类型的一部分。这是一个自包含的示例,因为我不知道您将uncurryN
,unapply
等定义为:
type CurriedFunction3<T1, T2, T3, R> = (a: T1, b: T2, c: T3) => R;
type CurriedSorter<T> = CurriedFunction3<(obj: T) => any, string, T[], T[]>;
type CurriedSorterValue = {
<T>(a: (obj: T) => any, b: string, c: T[]): T[];
}
const sortFactory: CurriedSorterValue = function (a, b, c) {
return c;
}