knp*_*wrs 5 javascript typescript ramda.js
我有一个通用接口可供我使用:
CurriedFunction3<T1, T2, T3, R>: R
Run Code Online (Sandbox Code Playgroud)
我想创建一个 const 绑定来填充其中一些类型参数。像这样的东西:
type CurriedSorter<T> = CurriedFunction3<(obj: T) => any, string, T[], T[]>;
Run Code Online (Sandbox Code Playgroud)
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),
)),
)));
Run Code Online (Sandbox Code Playgroud)
泛型类型
CurriedSorter需要 1 个类型参数。
的const结合sortFactory应当是用一个通用类型参数,被用作这样的功能:
sortFactory<MyType>(
prop('name'),
'DESC',
[{ name: 'foo' }, { name: 'bar' }]
) // returns `[{ name: 'bar' }, { name: 'foo' }]`
Run Code Online (Sandbox Code Playgroud)
如何在 TypeScript 中一般键入变量绑定?有没有办法用 TypeScript 做到这一点?
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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
138 次 |
| 最近记录: |